问题
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?
回答1:
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)
来源:https://stackoverflow.com/questions/6087731/comprehensive-list-of-python-protocols-interfaces