kindLOGIN

kind

1. Define a class

The syntax format of class definition is as follows:

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

A class is also composed of attributes and methods. Sometimes we need to Set the attributes of the class, so this requires a constructor

The constructor of the class is as follows:

def __init__(self,[...):

If the class defines the init() method, the instantiation operation of the class will be automatically called init() method.

Then if the constructor corresponds to the destructor, of course, when a class is created, we can use the constructor to set properties, then when a class is destroyed, the destructor will be called.

The syntax of the destructor is as follows:

def __del__(self,[...):

Children who observe carefully will find that there is a special difference between class methods and ordinary functions. They must have an additional first parameter name. , by convention its name is self.

So what does self represent?

We can look at examples and find out the answer through examples:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
class Test:
    def prt(self):
        print(self)
        print(self.__class__)
t = Test()
t.prt()

Observe the output results:

776927ef1b515b00d2ff42650fd27f4.pngIt can be clearly seen from the execution results, self represents an instance of a class, outputs the address of the current object, and self.__class__ points to the class.

Of course self is not a python keyword, which means that it can be executed normally if we replace it with other characters. It’s just that we are used to using self

2. Historical issues in defining classes in Python

In the iteration of Python versions, there is a question about classes The historical issues left over are the issues between new-style classes and old-style classes. Specifically, look at the following code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 旧式类
class OldClass:
    pass
# 新式类
class NewClass(object):
    pass

As you can see, different ways of defining classes are used here, and you can see the biggest difference. That is, the new-style class inherits the object class. In Python2, it is best to define the new-style class when we define a class. Of course, this problem does not exist in Python3, because all classes in Python3 are new-style classes.

So what is the difference between new-style classes and old-style classes?

Run the following code:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# 旧式类
class OldClass:
    def __init__(self, account, name):
        self.account = account;
        self.name = name;
# 新式类
class NewClass(object):
    def __init__(self, account, name):
        self.account = account;
        self.name = name;
if __name__ == '__main__':
    old_class = OldClass(111111, 'OldClass')
    print(old_class)
    print(type(old_class))
    print(dir(old_class))
    print('\n')
    new_class=NewClass(222222,'NewClass')
    print(new_class)
    print(type(new_class))
    print(dir(new_class))

Carefully observe the output results and compare them, and you will be able to observe them. Note that the output results in Pyhton3 are exactly the same. Because there is no problem of new-style classes and old-style classes in Python3.

Next Section
submitReset Code
ChapterCourseware