31 3.3. Special method names The Python Language Reference, Release 3.9.5 New in version 3.7:$_{__getattr__}$ and$_{__dir__}$ module attributes. Changed in version 3.5:$_{__class__}$ module attribute is now writable. See also: PEP 562 - Module __getattr__ and __dir__ Describes the$_{__getattr__}$ and$_{__dir__}$ functions on modules. The following methods only apply when an instance of the class containing the method (a so-called descriptor class) appears in an owner class (the descriptor must be in either the owner’s class dictionary or in the class dictionary for one of its parents). In the examples below, “the attribute” refers to the attribute whose name is the key of the property in the owner class’$_{__dict__}$ . Implementing Descriptors object. __get__ ( self , instance , owner=None ) Called to get the attribute of the owner class (class attribute access) or of an instance of that class (instance attribute access). The optional owner argument is the owner class, while instance is the instance that the attribute was accessed through, or None when the attribute is accessed through the owner . This method should return the computed attribute value or raise an$_{AttributeError}$ exception. PEP 252 specifies that __get__() is callable with one or two arguments. Python’s own built-in descriptors support this specification; however, it is likely that some third-party tools have descriptors that require both arguments. Python’s own$_{__getattribute__()}$ implementation always passes in both arguments whether they are required or not. object. __set__ ( self , instance , value$_{)}$ Called to set the attribute on an instance instance of the owner class to a new value, value . Note, adding$_{__set__()}$ or$_{__delete__()}$ changes the kind of descriptor to a “data descriptor”. See Invoking Descriptors for more details. object. __delete__ ( self , instance$_{)}$ Called to delete the attribute on an instance instance of the owner class. object. __set_name__ ( self , owner , name$_{)}$ Called at the time the owning class owner is created. The descriptor has been assigned to name . Note: __set_name__() is only called implicitly as part of the$_{type}$constructor, so it will need to be called explicitly with the appropriate parameters when a descriptor is added to a class after initial creation: classA : pass descr =custom_descriptor() A.attr =descr descr.__set_name__(A, 'attr') See Creating the class object for more details. New in version 3.6. The attribute$_{__objclass__}$ is interpreted by the$_{inspect}$module as specifying the class where this object was defined (setting this appropriately can assist in runtime introspection of dynamic class attributes). For callables, it may indicate that an instance of the given type (or a subclass) is expected or required as the first positional argument (for example, CPython sets this attribute for unbound methods that are implemented in C).