Python特殊方法名称的完整列表?

时间:2018-07-27 17:00:25

标签: python python-3.x

我想知道是否有办法以编程方式列出所有特殊方法(例如__getattr____lt__),而不是去Python data model documentation并复制粘贴所有方法。由于我可以使用dir(builtins)来检索内置标识符列表,因此我希望还有一种特殊方法。

1 个答案:

答案 0 :(得分:6)

不,没有从语言本身中获取规范的方法。您对文档一无所知(这不是一件坏事)。

我的意思是,您总是可以这样做

>>> import re, requests
>>> set(re.findall('__\w+__', requests.get('https://docs.python.org/3/reference/datamodel.html').text))
set(['__abs__',
     '__add__',
     '__aenter__',
     '__aexit__',
     '__aiter__',
     '__and__',
     '__anext__',
     '__annotations__',
     '__await__',
     '__bases__',
     '__bool__',
     '__bytes__',
     '__call__',
     '__ceil__',
     '__class__',
     '__class_getitem__',
     '__classcell__',
     '__closure__',
     '__code__',
     '__complex__',
     '__contains__',
     '__defaults__',
     '__del__',
     '__delattr__',
     '__delete__',
     '__delitem__',
     '__dict__',
     '__dir__',
     '__divmod__',
     '__doc__',
     '__enter__',
     '__eq__',
     '__exit__',
     '__file__',
     '__float__',
     '__floor__',
     '__floordiv__',
     '__format__',
     '__func__',
     '__future__',
     '__ge__',
     '__get__',
     '__getattr__',
     '__getattribute__',
     '__getitem__',
     '__globals__',
     '__gt__',
     '__hash__',
     '__iadd__',
     '__iand__',
     '__ifloordiv__',
     '__ilshift__',
     '__imatmul__',
     '__imod__',
     '__import__',
     '__imul__',
     '__index__',
     '__init__',
     '__init_subclass__',
     '__instancecheck__',
     '__int__',
     '__invert__',
     '__ior__',
     '__ipow__',
     '__irshift__',
     '__isub__',
     '__iter__',
     '__itruediv__',
     '__ixor__',
     '__kwdefaults__',
     '__le__',
     '__len__',
     '__length_hint__',
     '__lshift__',
     '__lt__',
     '__matmul__',
     '__missing__',
     '__mod__',
     '__module__',
     '__mro__',
     '__mro_entries__',
     '__mul__',
     '__name__',
     '__ne__',
     '__neg__',
     '__new__',
     '__next__',
     '__objclass__',
     '__or__',
     '__pos__',
     '__pow__',
     '__prepare__',
     '__qualname__',
     '__radd__',
     '__rand__',
     '__rdivmod__',
     '__repr__',
     '__reversed__',
     '__rfloordiv__',
     '__rlshift__',
     '__rmatmul__',
     '__rmod__',
     '__rmul__',
     '__ror__',
     '__round__',
     '__rpow__',
     '__rrshift__',
     '__rshift__',
     '__rsub__',
     '__rtruediv__',
     '__rxor__',
     '__self__',
     '__set__',
     '__set_name__',
     '__setattr__',
     '__setitem__',
     '__slots__',
     '__str__',
     '__sub__',
     '__subclasscheck__',
     '__traceback__',
     '__truediv__',
     '__trunc__',
     '__weakref__',
     '__xor__'])