Get the App
SLTechnology News&Howtos  ›  Development  › 

Comparative Analysis of python Class variables and instance variables

Shulou Source: shulou.com Published: 2022-05-31 17:50:51 09月30日 Update

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!

Tags: Instance variable object name analysis result value content learning practical deeper between interest practicality practical simple operation method more friends category Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft OPPO Reno Redmi NVidia