Python class and instance variables (class and instance properties)

Neither class attributes nor class methods can be used directly outside the class like ordinary variables or functions. We can regard the class as an independent space, then the class attribute is actually a variable defined in the class body, and the class method is a function defined in the class body.

As mentioned in the previous chapter, in the class body, class attributes can be subdivided into the following three types according to the location of variable definition and the way of definition:

  1. In the class body and outside all functions: variables defined in this range are called class attributes or class variables;
  2. Inside all functions in the class body: variables defined in the form of "self. Variable name" are called instance attributes or instance variables;
  3. Inside all functions in the class body: variables defined in the way of "variable name = variable value" are called local variables.

Moreover, class methods can also be subdivided into instance methods, static methods and class methods, which will be introduced in detail in subsequent chapters.

So what are the differences between class variables, instance variables and local variables? Next, I will explain this problem in detail.

Class variable (class attribute)

Class variables refer to variables defined in a class but outside the methods of each class. for instance:

class CLanguage :
    # Two class variables are defined below
    name = "C Language Chinese network"
    add = "http://c.biancheng.net"
    # A say instance method is defined below
    def say(self, content):
        print(content)

In the above program, name and add belong to class variables.

The characteristic of class variables is that all instantiated objects of classes share class variables at the same time, that is, class variables exist as public resources in all instantiated objects. There are two ways to call class methods, either directly using the class name or using the instantiated object of the class.

For example, add the following code outside the CLanguage class:

#Call directly with class name
print(CLanguage.name)
print(CLanguage.add)
#Modify the value of class variable
CLanguage.name = "Python course"
CLanguage.add = "http://c.biancheng.net/python"
print(CLanguage.name)
print(CLanguage.add)

The running result of the program is:

C language Chinese network
http://c.biancheng.net
Python tutorial
http://c.biancheng.net/python

You can see that you can not only call the class variable, but also modify its value through the class name.

Of course, you can also use class objects to call class variables in your class (this method is not recommended, and the reason will be discussed later). For example, add the following code outside the CLanguage class:

clang = CLanguage()
print(clang.name)
print(clang.add)

Run the program and the result is:

C language Chinese network
http://c.biancheng.net

Note that since class variables are shared by all instantiated objects, modifying the value of class variables through class names will affect all instantiated objects. For example, add the following code outside the CLanguage class body:

print("Values of class variables in various objects before modification:")
clang1 = CLanguage()
print(clang1.name)
print(clang1.add)
clang2 = CLanguage()
print(clang2.name)
print(clang2.add)
print("After modification, the values of class variables in various objects:")
CLanguage.name = "Python course"
CLanguage.add = "http://c.biancheng.net/python"
print(clang1.name)
print(clang1.add)
print(clang2.name)
print(clang2.add)
The running result of the program is:

Values of class variables in various objects before modification:
C language Chinese network
http://c.biancheng.net
C language Chinese network
http://c.biancheng.net
After modification, the values of class variables in various objects:
Python tutorial
http://c.biancheng.net/python
Python tutorial
http://c.biancheng.net/python

Obviously, modifying class variables by class name will affect all instantiated objects (such as clang1 and clang2 here).

Note that class variables cannot be modified through class objects. The essence of assigning values to class variables through class objects is no longer to modify the value of class variables, but to define new instance variables for the object (which will be introduced in detail when talking about instance variables).

It is worth mentioning that in addition to accessing class variables through class names, you can also dynamically add class variables to classes and objects. Add the following code based on the class cluage, for example:

clang = CLanguage()
CLanguage.catalog = 13
print(clang.catalog)

The operation result is:

13

Instance variables (instance properties)

Instance variable refers to the variable defined in the form of "self. Variable name" within any class method. Its characteristic is that it only acts on the object calling the method. In addition, instance variables can only be accessed through object names, not class names.

for instance:

class CLanguage :
    def __init__(self):
        self.name = "C Language Chinese network"
        self.add = "http://c.biancheng.net"
    # A say instance method is defined below
    def say(self):
        self.catalog = 13
e. Add and catalog are instance variables. Among them, due to__ init__ The () function will be called automatically when creating the class object, while the say() method needs to be called manually by the class object. Therefore, all class objects of the class CLanguage contain name and add instance variables, while only class objects that call the say() method contain catalog instance variables.

For example, on the basis of the above code, add the following statement:
clang = CLanguage()
print(clang.name)
print(clang.add)
#Because the clang object does not call the say() method, it has no catalog variable. The following line of code will report an error
#print(clang.catalog)
clang2 = CLanguage()
print(clang2.name)
print(clang2.add)
#Only when say() is called can the catalog instance variable be owned
clang2.say()
print(clang2.catalog)

The operation result is:

C language Chinese network
http://c.biancheng.net
C language Chinese network
http://c.biancheng.net
13

As mentioned earlier, class variables can be accessed through class objects, but the value of class variables cannot be modified. This is because modifying the value of a class variable through a class object is not assigning a value to a class variable, but defining a new instance variable. For example, in addition to the CLanguage class, add the following program:

clang = CLanguage()
#clang accessing class variables
print(clang.name)
print(clang.add)
clang.name = "Python course"
clang.add = "http://c.biancheng.net/python"
#The value of the clang instance variable
print(clang.name)
print(clang.add)
#Value of class variable
print(CLanguage.name)
print(CLanguage.add)

The running result of the program is:

C language Chinese network
http://c.biancheng.net
Python tutorial
http://c.biancheng.net/python
C language Chinese network
http://c.biancheng.net

Obviously, the value of class variables cannot be modified through class objects. The essence is to add two instance variables, name and add, to the clang object.

In a class, instance variables and class variables can have the same name, but in this case, class variables cannot be called by using class objects, and instance variables will be preferred. This is also the reason why "calling class variables with object names" is not recommended.

In addition, unlike class variables, modifying the value of an instance variable through an object will not affect other instantiated objects of the class, let alone class variables with the same name. For example:

class CLanguage :
    name = "xxx"  #Class variable
    add = "http://"# class variable
    def __init__(self):
        self.name = "C Language Chinese network"   #Instance variable
        self.add = "http://c.biancheng.net "# instance variable
    # A say instance method is defined below
    def say(self):
        self.catalog = 13  #Instance variable
clang = CLanguage()
#Modify the instance variable of the clang object
clang.name = "python course"
clang.add = "http://c.biancheng.net/python"
print(clang.name)
print(clang.add)
clang2 = CLanguage()
print(clang2.name)
print(clang2.add)
#Output the value of class variable
print(CLanguage.name)
print(CLanguage.add)

The running result of the program is:

python tutorial
http://c.biancheng.net/python
C language Chinese network
http://c.biancheng.net
xxx
http://

Moreover, Python only supports adding instance variables for specific objects. For example, on the basis of the previous code, add the money instance variable to the clang object, and the implementation code is:

clang.money = 30
print(clang.money)

local variable

In addition to instance variables, local variables can also be defined in class methods. Unlike the former, local variables are directly defined in the way of "variable name = value", for example:

class CLanguage :
    # A say instance method is defined below
    def count(self,money):
        sale = 0.8*money
        print("The preferential price is:",sale)
clang = CLanguage()
clang.count(100)

Usually, local variables are defined for the realization of the function of the method of the class. It should be noted that local variables can only be used in the function. After the function is executed, the local variables will also be destroyed.

Reprinted from: http://c.biancheng.net/view/2283.html

Keywords: Python

Added by gp177 on Thu, 10 Feb 2022 01:43:22 +0200