You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following example fails on Python 3.8.10 for several reasons.
classPColor(Protocol):
@abstractmethoddefdraw(self) ->str:
...
defcomplex_method(self) ->int:
# some complex code hereclassNiceColor(PColor):
defdraw(self) ->str:
return"deep blue"classBadColor(PColor):
defdraw(self) ->str:
returnsuper().draw() # Error, no default implementationclassImplicitColor: # Note no 'PColor' base heredefdraw(self) ->str:
return"probably gray"defcomplex_method(self) ->int:
# class needs to implement thisnice: NiceColoranother: ImplicitColordefrepresent(c: PColor) ->None:
print(c.draw(), c.complex_method())
represent(nice) # OKrepresent(another) # Also OK
The first reason why this fails is a syntax error (actually two), namely the methods complex_method are missing the ellipses. If these are corrected, the example still throws a NameError, since 'nice' and 'another' are actually not defined.
The corrected example:
classPColor(Protocol):
@abstractmethoddefdraw(self) ->str:
...
defcomplex_method(self) ->int:
# some complex code here
...
classNiceColor(PColor):
defdraw(self) ->str:
return"deep blue"classBadColor(PColor):
defdraw(self) ->str:
returnsuper().draw() # Error, no default implementationclassImplicitColor: # Note no 'PColor' base heredefdraw(self) ->str:
return"probably gray"defcomplex_method(self) ->int:
# class needs to implement this
...
nice: NiceColor=NiceColor()
another: ImplicitColor=ImplicitColor()
defrepresent(c: PColor) ->None:
print(c.draw(), c.complex_method())
represent(nice) # OKrepresent(another) # Also OK
The text was updated successfully, but these errors were encountered:
Documentation
The following example fails on Python 3.8.10 for several reasons.
The first reason why this fails is a syntax error (actually two), namely the methods
complex_method
are missing the ellipses. If these are corrected, the example still throws a NameError, since 'nice' and 'another' are actually not defined.The corrected example:
The text was updated successfully, but these errors were encountered: