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

标签 python python-3.x

<分区>

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

最佳答案

不,没有一种规范的方法可以从语言本身获得它。您受困于文档(这不是坏事)。

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

>>> 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__'])

关于python - Python 特殊方法名称的完整列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51562492/

相关文章:

python - pyspark countApprox() 似乎与 count() 没有区别

python - h5py文件和pickle文件保存模型的区别

python - 如何计算特定字体和大小的字符串长度(以像素为单位)?

python - 找不到套接字服务器的匹配发行版

python-3.x - 在 Windows Python 3.7 上安装 pyrebase 失败

python-3.x - tifffile 无法解压缩 JPEG,因为 JPEG 不在 TIFF.DECOMPRESSORS 中

python - 无法通过 apache/httpd 从外部 IP 地址访问第二个 flask 服务器

python - numpy 逻辑中首次出现

python - 根据另一列中字符串的结尾,为每种类型的不同结尾设置新列中的值

python - 如何使用 Python Urwid 进行类似 UI 的聊天?