class A(object):
val = 10
def __init__(self, val):
self.val = val
def method(self):
return self.val
@classmethod
def class_method(cls):
return cls.val
@classmethod
def change_class_val(cls, val):
cls.val = val
@classmethod
def change_class_method(cls, func):
cls.class_method = func
a = A(5)
aa = A(5)
print(a.method())
print(a.class_method())
a.change_class_val(9)
print(a.class_method())
print(aa.class_method())
A.change_class_val(7)
print(a.class_method())
print(aa.class_method())
A.val = 8
print(a.class_method())
print(aa.class_method())
A.change_class_method(lambda cls: "new method!")
print(a.class_method())
print(aa.class_method())
Y2xhc3MgQShvYmplY3QpOgoJdmFsID0gMTAKCQoJZGVmIF9faW5pdF9fKHNlbGYsIHZhbCk6CgkJc2VsZi52YWwgPSB2YWwKCQoJZGVmIG1ldGhvZChzZWxmKToKCQlyZXR1cm4gc2VsZi52YWwKCQoJQGNsYXNzbWV0aG9kCglkZWYgY2xhc3NfbWV0aG9kKGNscyk6CgkJcmV0dXJuIGNscy52YWwKCQoJQGNsYXNzbWV0aG9kCglkZWYgY2hhbmdlX2NsYXNzX3ZhbChjbHMsIHZhbCk6CgkJY2xzLnZhbCA9IHZhbAoJCQoJQGNsYXNzbWV0aG9kCglkZWYgY2hhbmdlX2NsYXNzX21ldGhvZChjbHMsIGZ1bmMpOgoJCWNscy5jbGFzc19tZXRob2QgPSBmdW5jCgkJCmEgPSBBKDUpCmFhID0gQSg1KQpwcmludChhLm1ldGhvZCgpKQpwcmludChhLmNsYXNzX21ldGhvZCgpKQoKYS5jaGFuZ2VfY2xhc3NfdmFsKDkpCnByaW50KGEuY2xhc3NfbWV0aG9kKCkpCnByaW50KGFhLmNsYXNzX21ldGhvZCgpKQoKQS5jaGFuZ2VfY2xhc3NfdmFsKDcpCnByaW50KGEuY2xhc3NfbWV0aG9kKCkpCnByaW50KGFhLmNsYXNzX21ldGhvZCgpKQoKQS52YWwgPSA4CnByaW50KGEuY2xhc3NfbWV0aG9kKCkpCnByaW50KGFhLmNsYXNzX21ldGhvZCgpKQoKQS5jaGFuZ2VfY2xhc3NfbWV0aG9kKGxhbWJkYSBjbHM6ICJuZXcgbWV0aG9kISIpCnByaW50KGEuY2xhc3NfbWV0aG9kKCkpCnByaW50KGFhLmNsYXNzX21ldGhvZCgpKQ==