The difference between python class variables and instance variables

爱喝马黛茶的安东尼
Release: 2019-06-24 10:56:09
Original
3073 people have browsed it

The essence of Python variables: assigned.

1 Ordinary python variables (non-class related variables) are easy to understand. After being assigned a value, the variable exists and can be read and written.

2 The variables of the Python class (class variables and instance object variables) are assigned somewhere in a certain way, that is, they exist and can be read and written.

The difference between python class variables and instance variables

2.1 Python class variables are assigned values

(1) In the design of the class

Inside and outside the class, variable names can be assigned Assignment.

# DEF can be assigned through the point -to -order variable name of the class object, that is, the name of the class name.

(2) In the program,

class names can also be assigned values ​​through dot arithmetic of class objects (class names).

Related recommendations: "Python Video Tutorial"

2.2 Python instance object variables are assigned values

(1) Class design

The variable name can be assigned a value through self dot operation in def, not necessarily in init, but also in other called method functions.

(2) In the program,

variable names can be assigned values ​​through dot arithmetic of instance objects.

Generally, variables in a class are completed through class design (2.1(1) and 2.2(1)). Adding variables in a class generally does not use the method (2.1(2) and 2.2(2)). , completion (2.1(2) and 2.2(2)) can be achieved through class inheritance.

class aa:
      w = 10
      def __init__(self):
           self.x = 11
           self.y = 12
      def add(self):
           return self.x + self.y
a = aa()
print a.add()
#下边两条指令各起何作用?结果是输出两个 20 么?还是两个13?还是?
aa.w = 20 
a.w = 13
print aa.w, a.w
#程序继续增加如下,怎样理解这t和q呢?他们是___变量
a.t = 14
a.q = 15
print a.t, a.q
#程序继续增加如下,怎样理解这m和n呢?他们是___变量
aa.m = 30
aa.n = 40
print aa.m, aa.n
#好了再来个提升吧
#程序继续增加,下列三个print语句都能正确执行么?为何?
b = aa()
print b.x,b.y
print b.t,b.q
print b.m,b.n
Copy after login

To solve the above problem, we must first understand when does a class have class variables and instances have instance variables?

Secondly, we must deeply understand the role of class variables, and who is involved in the scope of instance variables!

The above is the detailed content of The difference between python class variables and instance variables. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!