List of all Python dunder methods - Which ones do you need to implement to correctly proxy an object?

ε祈祈猫儿з 提交于 2020-08-23 03:30:12

问题


I'm trying to create an object proxy. Attribute/property lookup can be done by simply implementing the __getattribute__, __setattr__ and __delattr__ methods. However, other functionalities like len(x), x[], bool(x) require other dunder methods like __len__, __getitem__, __bool__ to be implemented. If you don't implement these on the proxy class, but the object you're proxying supports them, your proxy will be incomplete and cause runtime errors.

I would therefore like to have a comprehensive list of all the things I need to implement, but I couldn't find any reliable list online.

Here's 97 unique dunder method names of them I got from the typing and builtins modules. I know what a lot of them do, but there are some that I have no clue about. It will be a pain to implement all or most of them for my proxy class, so I would be glad if there is a workaround.

__abs__
__add__
__aenter__
__aexit__
__aiter__
__and__
__anext__
__await__
__bool__
__bytes__
__call__
__class__
__cmp__
__complex__
__contains__
__delattr__
__delete__
__delitem__
__delslice__
__dir__
__div__
__divmod__
__enter__
__eq__
__exit__
__float__
__floordiv__
__format__
__fspath__
__ge__
__get__
__getattribute__
__getitem__
__getnewargs__
__getslice__
__gt__
__hash__
__iadd__
__iand__
__import__
__imul__
__index__
__init__
__init_subclass__
__instancecheck__
__int__
__invert__
__ior__
__isub__
__iter__
__ixor__
__le__
__len__
__lshift__
__lt__
__mod__
__mul__
__ne__
__neg__
__new__
__next__
__nonzero__
__or__
__pos__
__pow__
__prepare__
__radd__
__rand__
__rdiv__
__rdivmod__
__reduce__
__reduce_ex__
__repr__
__reversed__
__rfloordiv__
__rlshift__
__rmod__
__rmul__
__ror__
__round__
__rpow__
__rrshift__
__rshift__
__rsub__
__rtruediv__
__rxor__
__set__
__setattr__
__setitem__
__setslice__
__sizeof__
__str__
__sub__
__subclasscheck__
__subclasses__
__truediv__
__xor__

来源:https://stackoverflow.com/questions/56238263/list-of-all-python-dunder-methods-which-ones-do-you-need-to-implement-to-corre

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