fork download
  1. class A(object):
  2. val = 10
  3.  
  4. def __init__(self, val):
  5. self.val = val
  6.  
  7. def method(self):
  8. return self.val
  9.  
  10. @classmethod
  11. def class_method(cls):
  12. return cls.val
  13.  
  14. @classmethod
  15. def change_class_val(cls, val):
  16. cls.val = val
  17.  
  18. @classmethod
  19. def change_class_method(cls, func):
  20. cls.class_method = func
  21.  
  22. a = A(5)
  23. aa = A(5)
  24. print(a.method())
  25. print(a.class_method())
  26.  
  27. a.change_class_val(9)
  28. print(a.class_method())
  29. print(aa.class_method())
  30.  
  31. A.change_class_val(7)
  32. print(a.class_method())
  33. print(aa.class_method())
  34.  
  35. A.val = 8
  36. print(a.class_method())
  37. print(aa.class_method())
  38.  
  39. A.change_class_method(lambda cls: "new method!")
  40. print(a.class_method())
  41. print(aa.class_method())
Success #stdin #stdout 0.03s 9984KB
stdin
Standard input is empty
stdout
5
10
9
9
7
7
8
8
new method!
new method!