Comprehensive list of Python protocols/interfaces

為{幸葍}努か 提交于 2019-12-22 02:50:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!