Lately, I was looking at some Python idioms.
I found many descriptions of protocols used in Python, such as the ordering (__cmp__
, ...) or generators. Besides, there are also methods like __hash__
which are defined for every object (I suppose).
After some search on the internet, I haven't found a comprehensive list of these protocols and methods.
Can anyone give me some pointers URLs?
Protocol class was added to Python 3.8 as part of PEP 544 as a mechanism for “structural subtyping.” Basically, it is used to define an interface class that acts as a blueprint for designing other classes. Like classes, interface classes define methods however Unlike classes, these methods are abstract methods.
Protocol classes allow us to define an interface, called a protocol, and use static type-checking via mypy to verify that objects satisfy the interface – without classes having to declare that they satisfy the interface or subclass anything.
Your best reference is always going to be the Python Online Documentation, specifically the section on Special method names.
The interactive Python interpretor is a very useful tool, too. Try some of these:
>>> dir(object)
['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> help(object.__class__)
>>> help(object.__hash__)
>>> help(hash)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With