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
Python nowadays supports "protocols". Protocols are mostly a typing thing, and allow you to specify that a class follows the protocol without explicitly being a subclass.
When using the BMI as an abstract base class, you currently need to use the following syntax:
This is because type checkers will check if BmiImplementation follows the BmiProtocol: does it have all methods that are in the protocol, and do all the methods have the correct typing.
To implement this, all that we would need to change is;
fromtypingimportProtocolclassBmi(Protocol):
Note that you can still subclass from Protocol like you could from an ABC.
The text was updated successfully, but these errors were encountered:
Python nowadays supports "protocols". Protocols are mostly a typing thing, and allow you to specify that a class follows the protocol without explicitly being a subclass.
When using the BMI as an abstract base class, you currently need to use the following syntax:
If you don't subclass BMI, type checkers will get angry at this, as
BmiImplementation
is not a subclass ofBmiABC
:However, if you specify that
Bmi
is a subclass oftyping.protocol
, the following syntax passes type checkers:This is because type checkers will check if
BmiImplementation
follows theBmiProtocol
: does it have all methods that are in the protocol, and do all the methods have the correct typing.To implement this, all that we would need to change is;
Note that you can still subclass from Protocol like you could from an ABC.
The text was updated successfully, but these errors were encountered: