Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comprehensive list of Python protocols/interfaces

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?

like image 656
Kru Avatar asked May 22 '11 11:05

Kru


People also ask

What are the protocols in Python?

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.

What is a protocol class?

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.


1 Answers

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)
like image 83
Johnsyweb Avatar answered Sep 27 '22 21:09

Johnsyweb