python Modify class properties
1, When the class property is immutable The value of , Can not be Modify class properties through instance objects
class Foo(object):
x = 1.5
foo = Foo()
print(foo.x) # Output :1.5
print(id(foo.x)) # Output :2400205363696
foo.x = 1.7
print(foo.x) # Output :1.7
print(id(foo.x)) # Output :2400203142352 And the above id Dissimilarity , Indicates that an instance property has been created
print(Foo.x) # Output :1.5
2, When the class property is variable The value of , Sure Modify class properties through instance objects
class Foo(object):
x = [1,2,3]
foo = Foo()
print(foo.x) # Output :[1, 2, 3]
print(id(foo.x)) # Output :1999225501888
foo.x[2] = 4
print(foo.x) # Output :[1, 2, 4]
print(id(foo.x)) # Output :1999225501888
print(Foo.x) # Output :[1, 2, 4]
foo.x.append(5)
print(foo.x) # Output :[1, 2, 4, 5]
print(id(foo.x)) # Output :1999225501888
print(Foo.x) # Output :[1, 2, 4, 5]