fork download
  1. from inspect import ismethod
  2.  
  3. class A:
  4. @property
  5. def prop(self) -> int:
  6. return 1
  7.  
  8. @property
  9. def other_prop(self) -> int:
  10. return self.prop * 4 # Uses self.prop
  11.  
  12. def method(self) -> int:
  13. return self.prop * 2 # Uses self.prop
  14.  
  15. class B:
  16. def __init__(self, a: A):
  17. self._a = a
  18.  
  19. @property
  20. def prop(self) -> int:
  21. return 2 # Override A's property
  22.  
  23. def __getattr__(self, attr):
  24. value = getattr(type(self._a), attr, None)
  25. if isinstance(value, property):
  26. return value.fget(self)
  27. value = getattr(self._a, attr)
  28. if ismethod(value):
  29. value = value.__func__.__get__(self)
  30. return value
  31.  
  32. b = B(A())
  33. print(b.prop)
  34. print(b.method())
  35. print(b.other_prop)
Success #stdin #stdout 0.19s 18600KB
stdin
Standard input is empty
stdout
2
4
8