builtin_property_inherit.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # test builtin property combined with inheritance
  2. try:
  3. property
  4. except:
  5. print("SKIP")
  6. raise SystemExit
  7. # test property in a base class works for derived classes
  8. class A:
  9. @property
  10. def x(self):
  11. print('A x')
  12. return 123
  13. class B(A):
  14. pass
  15. class C(B):
  16. pass
  17. class D:
  18. pass
  19. class E(C, D):
  20. pass
  21. print(A().x)
  22. print(B().x)
  23. print(C().x)
  24. print(E().x)
  25. # test that we can add a property to base class after creation
  26. class F:
  27. pass
  28. F.foo = property(lambda self: print('foo get'))
  29. class G(F):
  30. pass
  31. F().foo
  32. G().foo
  33. # should be able to add a property to already-subclassed class because it already has one
  34. F.bar = property(lambda self: print('bar get'))
  35. F().bar
  36. G().bar
  37. # test case where class (H here) is already subclassed before adding attributes
  38. class H:
  39. pass
  40. class I(H):
  41. pass
  42. # should be able to add a normal member to already-subclassed class
  43. H.val = 2
  44. print(I().val)
  45. # should be able to add a property to the derived class
  46. I.baz = property(lambda self: print('baz get'))
  47. I().baz