The use of classes in Python 1

A class in Python is a data type that combines data and functionality.

1 Definition of class

The syntax for defining a class is as follows:

class ClassName:
    <statement-1>
    ...
    <statement-N>

Among them, class is the keyword to define the class, ClassName represents the class name, followed by a colon, and then statement-1 to statement-N represent the statement defining the class within the scope of defining the class, which contains the data and functions of the class.

2 types of data and functions

The main feature of a class is to integrate data and functions, where the data is the variable of the class, and the function is the method of the class. The variables and methods of Python classes are mainly divided into two types: one is the variables and methods of the class, and the other is the variables and methods of the instance.

2.1 Class variables and methods

Define a class named MyClass, the code is as follows:

class MyClass:
    i = 1
    def myfunc():
        print('Hello')

As can be seen from the above code, the MyClass class contains a class variable i and a class method myfunc. When calling, you need to use the method of "class name. variable name" or "class name. method name". The code is as follows.

print(MyClass.i)
MyClass.myfunc()

Run the program, you can output "1" and "Hello".

The variables of the class can be modified by "class name. variable name", the code is as follows:

MyClass.i = 2
print(MyClass.i)

The output at this time is "2".

2.2 Instance variables and methods

A class is a data type that can define multiple variables of the class (data type). The process of defining a class variable is called "instantiation", and the defined class variable is called an "instance".

c1 = MyClass()

At this point, c1 is an instance of the MyClass class. You can perform different initialization operations on different classes by defining the __init__() method of the MyClass class.

class MyClass:
    i = 1
    def myfunc():
        print('Hello')
    def __init__(self, num):
        self.j = num

As can be seen from the above code, MyClas has added a method named __init__(), which has two parameters, one is self, which indicates the instance object that calls the method, and the other parameter is num, which is used when calling __init__( ) function passed in.

When an instance of a class is defined, the __init__() method of the class is automatically called, and the created variable is used as the first parameter of the function, that is, the value of the parameter self, and the parameter in parentheses after the class name is __init__( ) method other parameter values. The code looks like this:

c1 = MyClass(10)

The above code defines an instance of the MyClass class. When using this code to create an instance, the __init__() function of the MyClass class is automatically called. At this time, the value of the parameter self of the __init__() function is "c1", and the value of the parameter num The value is "10". Execute the __init__() method of the MyClass class, and set the value of the variable j of the instance c1 to 10. The format of using instance variables is "instance name.variable name", execute the following code:

print(c1.j)

The output is "10". The variable j in the MyClass class definition is an "instance variable". For an "instance variable", different instances can have different values. For example:

c2 = MyClass(100)

At this point, c2 is also an instance of MyClass, execute the following code:

print(c2.j)

The output result is "100".

Guess you like

Origin blog.csdn.net/hou09tian/article/details/131223781