crunk1
crunk1

Reputation: 2818

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 emulate it by creating a class property AND an instance property, but it screws with PyCharm's type hinting. Here's what I consider the ideal set up:

class Meta(type):
    @property
    def cls_prop(cls) -> str:
        return 'foo'


class A(metaclass=Meta):
    pass

But unfortunately, here are the results:

>>> A.cls_prop
'foo'
>>> a = A()
>>> a.cls_prop
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'cls_prop'

Adding an instance property to A that calls to the class property works at runtime, but PyCharm's type introspection gets confused (it starts treating A.cls_prop as a property instead of a str):

class A(metaclass=Meta):
  @property
  def cls_prop(self) -> str:
      return self.__class__.cls_prop

>>> A.cls_prop
'foo'
>>> a = A()
>>> a.cls_prop
'foo'

Is there a better way to do this?

Upvotes: 4

Views: 1147

Answers (2)

jsbueno
jsbueno

Reputation: 110301

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.

Upvotes: 0

soulmerge
soulmerge

Reputation: 75704

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

Upvotes: 1

Related Questions