0

I know how to use property in case of instance variable

class A():
    _foo=3
    @property
    def foo(self):
        return self.foo
a = A()
print(a.foo)

it show _foo

3

but i don't know how to use property in case of class variable

class A():
    _foo=3
    @property
    def foo(cls):
        return cls._foo
print(A.foo)

it show property object not 3

<property object at 0x7fba8c19ea70>

how to generate class variable getter/setter?

I found a way in here

class MetaFoo(type):
    @property
    def thingy(cls):
        return cls._thingy

class Foo(object, metaclass=MetaFoo):
    _thingy = 23
    
print(Foo.thingy)

it show

23

but i want more simple ways.

권순규
  • 33
  • 5
  • If you want `print(A.foo)` gives you `3`, why not just use `foo=3` under your class definition? Using the `property` decorator seems a bit redundant. I don't get what you want to achieve here. – fusion Aug 31 '20 at 07:07
  • Because I do use setter not getter. And in foo.setter function, I want to assign cls._bar, cls._foobar, cls._etc as well as cls._foo. In other words, I want when I change cls._foo, cls._bar, cls.foobar, cls.etc are changed also. – 권순규 Aug 31 '20 at 07:36

0 Answers0