fork(1) download
  1. class classproperty(property):
  2. def __get__(self, owner_self, owner_cls):
  3. return self.fget(owner_cls)
  4.  
  5. class CMeta(type):
  6. def __setattr__(cls, name, value):
  7. if isinstance(vars(cls).get(name), classproperty):
  8. raise AttributeError("can't set attribute")
  9. super().__setattr__(name, value)
  10.  
  11. class C(metaclass=CMeta):
  12. @classproperty
  13. def x(cls):
  14. return 1
  15.  
  16. print(C.x)
  17. print(C().x)
  18. C.x = 2 # AttributeError: can't set attribute
Runtime error #stdin #stdout #stderr 0.92s 37552KB
stdin
Standard input is empty
stdout
1
1
stderr
Traceback (most recent call last):
  File "./prog.py", line 18, in <module>
  File "./prog.py", line 8, in __setattr__
AttributeError: can't set attribute