Skip to content Skip to sidebar Skip to footer

Python Metaclass - Make Class Property Accessible Via Class And Class Instance

Using python 3.7, I have created a class property in a metaclass. I would like to be able to access the property via the class itself or an instantiated object of the class. I can

Solution 1:

I think what you are trying to do is better accomplished using a parent class:

class Parent:
    @property
    def cls_prop(self):
        return 'foo'

class A(Parent):
    pass

>>> a = A()
>>> a.cls_prop
'foo'
>>>> A.cls_prop
<property object at 0x7f1afcb190e8>

If you also want to be able to access A.cls_prop directly on the class (i.e. without creating an instance), you might want to look at this other question: @staticmethod with @property


Solution 2:

The problem you are facing is that property, howver a handy thing in Python, is not designed to fullfill all possible use cases.

It is a nice use of the descriptor protocol, and is meant for instance properties, and just remains accessible as itself when called from the class.

Having an anologue to property that would work both from the class and from the instance is just a matter of writting a class with a proper __get__ method - for a fixed string, it could be as simples as:

class MyProperty:
    def __init__(self, value):
        self.value = value
    def __get__(self, instance, owner):
         return self.value 

class MyClass:
   cls_prop = MyProperty("foo")

(the big difference to property is returning the value even is "instance" is None)

And, if you want, you can re-implement property 's niceties, such as be able to work as a decorator, and have decorators for the setter and deleter as well.


Post a Comment for "Python Metaclass - Make Class Property Accessible Via Class And Class Instance"