Object-oriented Advanced
1. Class variables and instance variables
#One, variable#variables include: instance variables and class variables,#they differ in their definition and use, and the most essential difference is that they are stored in different places in memory .#instance variable belongs to object#class variables belong to classclassProvince (object): Country="China" #class Variables def __init__(self, name): Self.name= Name#instance Variable#direct access to instance variables (instance names to invoke)obj = Province ('Hebei province')Print(Obj.name)#Hebei province#direct access to class variables (class name to invoke)Print(Province.country)#China#as you can see from the code above, "instance variables need to be accessed through objects to access" class variables through class access .#It can be seen that the attribution of class variables and instance variables is different. #class variables save only one copy in memory#instance variables are saved in one copy of each object#class variable scenario: When you create an object from a class, the class field is used if each object has the same field
View Code
Python "day7": Python Learning (object-oriented advanced, reflection, Socket Introduction)