Difference between __getattr__ vs __getattribute__

前端 未结 8 1252
甜味超标
甜味超标 2020-11-22 12:54

I am trying to understand when to use __getattr__ or __getattribute__. The documentation mentions __getattribute__ applies to new-sty

8条回答
  •  無奈伤痛
    2020-11-22 13:17

    • getattribute: Is used to retrieve an attribute from an instance. It captures every attempt to access an instance attribute by using dot notation or getattr() built-in function.
    • getattr: Is executed as the last resource when attribute is not found in an object. You can choose to return a default value or to raise AttributeError.

    Going back to the __getattribute__ function; if the default implementation was not overridden; the following checks are done when executing the method:

    • Check if there is a descriptor with the same name (attribute name) defined in any class in the MRO chain (method object resolution)
    • Then looks into the instance’s namespace
    • Then looks into the class namespace
    • Then into each base’s namespace and so on.
    • Finally, if not found, the default implementation calls the fallback getattr() method of the instance and it raises an AttributeError exception as default implementation.

    This is the actual implementation of the object.__getattribute__ method:

    .. c:function:: PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name) Generic attribute getter function that is meant to be put into a type object's tp_getattro slot. It looks for a descriptor in the dictionary of classes in the object's MRO as well as an attribute in the object's :attr:~object.dict (if present). As outlined in :ref:descriptors, data descriptors take preference over instance attributes, while non-data descriptors don't. Otherwise, an :exc:AttributeError is raised.

提交回复
热议问题