Understanding of self in Python class (class)

0. Preface

In accordance with international practice, I first declare: This article is just my own understanding. Although I have referred to other people's valuable insights, there are many inaccuracies in the content. I hope to criticize and correct and make progress together.

For Python novices (even some veterans), in object-oriented programming classes (class), there is a more troublesome parameter - self.

Therefore, writing this article strengthens your own understanding and helps others understand.

1. Understand "self" from a question

The following is a piece of python code about class. There is no "self" parameter in this piece of code. Please think about which of the 4 "print" in this piece of code can be run, and which one will report an error?

The answer will be revealed later.

class test():
    def __init__(x):
        x.ten = 10

    def multi_ten(y, z):
        return z*y.ten

    def divide_ten(w, v):
        return v/w.ten

    def hello(k):
        return 'hello'

    def world():
        return 'world'

TEST = test()

print(TEST.multi_ten(9))
print(TEST.divide_ten(100))
print(TEST.hello())
print(TEST.world())

2. Definition of "self"

Python stipulates that in the method (method) of the class (class), there must be a formal parameter (parameter), and it must be 第一个形参,用于传递类的实例(instance). And this formal parameter is generally agreed to be named "self".

The functions in a class are called methods, for example, the multi_ten() and divide_ten() above are all methods. In Pycharm, when writing a method in a class, the "self" parameter will also be automatically jumped out.

Take the above example, in the multi_ten() method, the first parameter (parameter) is "y", after the test() class is instantiated, the actual parameter (argument) it passes is the "TEST" instance (instance), so "y" here is equivalent to "self".

Because the actual parameter "TEST" has been passed into the multi_ten() method, the "TEST" instance can call multi_ten(), that is, TEST.multi_ten().

3. __init__()Method

__init__()It is essentially a method, so it has little to do with "self" (formal parameter). Why are the two often linked together?

Because __init__()it is a special method, __init__()it will run automatically every time a new instance of the class is created, so that __init__()variables prefixed with "self" can be defined in the method, such as x.ten above, for use by all methods in the class.

4. Problem Analysis

Going back to the original question, through the understanding of "self", we can know that the first three prints can be run, and the last one will report an error.

C:\Users\Lenovo\Desktop\DL\Pytest\Scripts\python.exe C:/Users/Lenovo/Desktop/DL/Pytest/test_main.py
90
10.0
hello
Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\DL\Pytest\test_main.py", line 22, in <module>
    print(TEST.world())
TypeError: world() takes 0 positional arguments but 1 was given

Process finished with exit code 1

And the reason for the error "TypeError: world() takes 0 positional arguments but 1 was given" is to say again: when defining the world() method, the positional parameter (positional parameter) was not filled in, and it should also give 0 when calling A positional argument (positional argument), but the actual use gave a positional argument (positional argument) - "TEST", so an error will be reported.

5 Conclusion

Finally, let me explain that although Python classes can be replaced by other variables in the "self", this operation is extremely not recommended. No matter the novice or veteran, in any case, the "self" position should be Honestly written as "self". The code above should read like this:

class test():
    def __init__(self):
        self.ten = 10

    def multi_ten(self, z):
        return z*self.ten

    def divide_ten(self, v):
        return v/self.ten

    def hello(self):
        return 'hello'

    def world(self):
        return 'world'

TEST = test()

print(TEST.multi_ten(9))
print(TEST.divide_ten(100))
print(TEST.hello())
print(TEST.world())

Guess you like

Origin blog.csdn.net/m0_49963403/article/details/129478146