# class fun(): # a = 1 # class attribute # def l(self): # a = 4 # print('a') # b = fun() # print(fun.a) # class Test(object): # name = 'scolia' # # a = Test() # Test.name = 'scolia good' # Modify class attributes through classes, success! Even the instance properties have been changed # print(Test.name) # print(a.name) # class Test(object): # name = 'scolia' # # a = Test() # a.name = 'scolia good' # Modifying through an instance is unsuccessful, but it is equivalent to re-creating the instance attribute without modifying the class attribute # print(Test.name) # print(a.name) # The situation here is that I access a property in the instance, but I don't have it in the instance, so I try to create my class to find out if there is this property. # If found, there is, if not found, throw an exception. (This shows that instance objects can access class properties! Conversely, class objects cannot access instance properties!) # And when I try to use the instance to modify a property that is immutable in the class, I don't actually modify it, but create the property in my instance. # And when I access this property again, I have it in the instance, so I don't have to look for it in the class. # class Test(object): # name = 'scolia' # # a = Test() # a.abc = 123 # print(dir(Test)) # abc is automatically created in instance properties # print (dir) # Having confirmed that instance properties can be modified by class objects, try modifying class properties by instance # class Test: # list1 = [] # # a = Test() # a.list1.append(123) # Modify the list in the class by instance # print(Test.list1) # print(a.list1) # You can also arbitrarily add methods to an instance, python supports adding attributes dynamically # class Test: # pass # # def fangfa(): # print('I am a method of an instance') # # a = Test() # b = Test() # a.abc = fangfa # Deliberately add a method # a.abc() # b.abc() # b does not have this method # You can also dynamically add methods to classes class Test: pass def fangfa(self): # self represents an instance method, which can only be called by an instance print('I am a method') Test.abc = fangfa a = Test() a.abc() b = Test() # The method of the class has been modified (added) b.abc()