Comparative Analysis of python Class variables and instance variables
This article mainly explains "the instance comparative analysis of python class variables and instance variables". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "python class variables and instance variables of the case comparative analysis" bar!
Difference
1. Class variables are common to all objects, one of which changes its value, and the other objects get the result of the change.
2. Instance variables are private to objects, and one object changes its value without affecting other objects.
Class variable
You can use it directly without an instance, which is equivalent to binding to a class instead of binding to an instance. However, class variables can also be called in an instance. Values that can be shared between all category instances.
Class Human: name = 'first name' # Class variable print (Human.name) human = Human () print (human.name) execution result: first name
Instance variable
Variables owned separately by each instance after instantiation.
Class Human: def _ init__ (self, name): self.name = name # instance variable human = Human ('name') print (human.name) execution result: name so far, I believe you have a deeper understanding of "instance comparative analysis of python variables and instance variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!