Python class variable and object variable declaration analysis

Python class variable and object variable declaration analysis

Original link: https://www.cnblogs.com/bwangel23/p/4330268.html


Python classes, like C++, also have two types of variables, class variables and object variables! The former is owned by the class and shared by all objects, while the latter is unique to each object. Here I mainly want to discuss their declaration method.

The first thing to talk about is class variables :

As long as it is declared in the statement block of the class and does not have the " self. " prefix, it is a class variable , and class variables are shared by all objects.

Pay attention to the red part. If it is declared in the statement block of a class method , it is a local variable ! For example, the following example:

#!/usr/bin/env python
# -* - coding: UTF-8 -* -
#Function: Use the class var

class Person:
    cvar = 1 # 类的变量
    def sayHi(self):
      fvar = 1 # 方法sayHi()中的局部变量

print(Person.cvar)
print(Person.fvar)

That cvar is a variable belonging to the Python class, and that fvar is a local variable in the method sayHi(). An error will be reported in the 11th statement!

Next, let’s discuss how to declare object variables :

Variables starting with " self. " declared in the statement block of a class method are all object variables , unique to the object!

For example, the following example

#!/usr/bin/env python
# -* - coding: UTF-8 -* -
#Function: Use the object var

class Person:
    def haveName(self):
      self.name = 'Michael'
    def sayName(self):
      print(self.name)

def main():
    p = Person()

    p.haveName()
    p.sayName()

main()

Here, an object variable is declared in the haveName() method, and then called in the sayName() method. Then the main program will output it!

However, it is recommended to declare the object variable in the __init__() method, because this method will be called as soon as the object is created. Otherwise, such as the above example, if I call sayName() first, an error will occur, saying The object instance does not have the name attribute yet!

The last thing I want to say is that there are no keywords such as private public in Python to indicate the access permissions of class variables or methods, but you can indicate that the member is private by the class by adding "__" in front of the variable or method. , cannot be called externally, such as the following example:

#!/usr/bin/env python
# -* - coding: UTF-8 -* -
#Function: Use the private var and func

class Person:
    __count = 0            #这个变量是私有数据成员,只能被类的方法访问,是属于类的
    def get(self):
      return(Person.__count)
    def __pri(self):
      print('Yes')

p = Person()
print(p.get())

p.__pri()
print(p.__count)

For example, the class variable __count here is private to the class and can only be called by function members of the class (line 13). Calling it outside the class (line 16) is wrong! Also, the function member __pri() is also private, and it is wrong to call it directly outside the class (line 15)!

Guess you like

Origin blog.csdn.net/wagnbo/article/details/129164823