Search code examples
pythonclasspropertiesattributeerrormetaclass

Why does getting a class property from an instance raise an AttributeError?


Normally, you can access a regular class attribute/field from an instance of that class. However, when trying to access a class property, an AttributeError is raised. Why can't the instance see the property on the class object?

class Meta(type):

    @property
    def cls_prop(cls):
        return True


class A(metaclass=Meta):
    cls_attr = True


A.cls_attr # True
A.cls_prop # True
a = A()
a.cls_attr # True
a.cls_prop # AttributeError: 'A' object has no attribute 'cls_prop'

Solution

  • That is due to the way attribute lookup works. Upon trying to retrieve an attribute in an instance, Python does:

    1. call the instance's class __getattribute__(not the metaclass __getattribute__), which in turn will:

      1. Check the instance's class, and its superclasses following the method resolution order, for the attribute. It does not proceed to the class of the class (the metaclass) - it follows the inheritance chain..

        1. if the attribute is found in the class and it has a __get__ method, making it a descriptor: the __get__ method is called with the instance and its class as parameters - the returned value is used as the attribute value
          • note: for classes using __slots__, each instance attribute is recorded in a special descriptor - which exists in the class itself and has a __get__ method, so instance attributes for slotted classes are retrieved at this step
        2. if there is no __get__ method, it just skips the search at the class.
      2. check the instance itself: the attribute should exist as an entry in the instances __dict__ attribute. If so, the corresponding value is returned. (__dict__ is an special attribute which is accessed directly in cPython, but would otherwise follow the descriptor rule for slotted attributes, above)

      3. The class (and its inheritance hierarchy) are checked again for the attribute, this time, regardless of it having a __get__ method. If found, that is used. This attribute check in the class is performed directly in the class and its superclasses __dict__, not by calling their own __getattribute__ in a recursive fashion. (*)

    2. The class (or superclasses) __getattr__ method is called, if it exists, with the attribute name. It may return a value, or raise AttributeError(__getattr__ is a different thing from the low level __getattribute__, and easier to customize)

    3. AttributeError is raised.

    (*) This is the step that answers your question: the metaclass is not searched for an attribute in the instance. In your code above, if you try to use A.cls_prop as a property, instead of A().cls_prop it will work: when retrieving an attribute directly from the class, it takes the role of "instance" in the retrieval algorithm above.

    (**) NB. This attribute retrieval algorithm description is fairly complete, but for attribute assignment and deletion, instead of retrieval, there are some differences for a descriptor, based on whether it features a __set__ (or __del__) method, making it a "data descriptor" or not: non-data descriptors (such as regular methods, defined in the instance's class body), are assigned directly on the instance's dict, therefore overriding and "switching off" a method just for that instance. Data descriptors will have their __set__ method called.

    how to make properties defined in the metaclass work for instances:

    As you can see, attribute access is very customizable, and if you want to define "class properties" in a metaclass that will work from the instance, it is easy to customize your code so that it works. One way is to add to your baseclass (not the metaclass), a __getattr__ that will lookup custom descriptors on the metaclass and call them:

    class Base(metaclass=Meta):
        def __getattr__(self, name):
            metacls = type(cls:=type(self))
            if hasattr(metacls, name):
                metaattr = getattr(metacls, name)
                if isinstance(metaattr, property):  # customize this check as you want. It is better not to call it for anything that has a `__get__`, as it would retrieve metaclass specific stuff, such as its __init__ and __call__ methods, if those were not defined in the class.
                    attr = metaattr.__get__(cls, metacls)
                    return attr
            return super().__getattr__(name)
    

    and:

    In [44]: class A(Base):
        ...:     pass
        ...:
    
    In [45]: a = A()
    
    In [46]: a.cls_prop
    Out[46]: True