SoFunction
Updated on 2024-10-30

Python self parameter details

In defining a class, either explicitly creating the class's constructor methods or adding instance methods to the class, the self parameter is required as the first argument to the method. For example, define a Person class:

class Person:
def__init__(self):
print("Constructor being executed.")
# Define a study() instance method
defstudy(self,name):
print(name,"Learning Python.")

So what is the role of self? This section describes the self argument in detail.

In fact, Python only specifies that both constructor and instance methods should contain at least one parameter, and does not specify a specific name for that parameter. The reason why it is named self is just a convention among programmers, and adhering to this convention makes the code we write more readable (as soon as you see self, you know what it does).

So what exactly does the self argument do? Let's say that if you compare a class to the drawing of a house, then the objects instantiated by the class are the actual houses that you can live in. Based on a single drawing (class), we can design thousands of houses (class objects), each of which looks similar (they all have the same class variables and class methods), but they all have their own owners, so how can we distinguish between them?

Of course, it is through the self parameter, which is the equivalent of a key to the door of each house, which ensures that the owner of each house can only enter his or her own house (each class object can only call its own class variables and class methods).

If you've worked with other object-oriented programming languages (such as C++), the self argument to Python class methods is actually the equivalent of the this pointer in C++.

That is, the same class can produce multiple objects, and when an object calls a class method, that object automatically passes a reference to itself as the first parameter to that method; in other words, Python automatically binds the first parameter of the class method to point to the object that called the method. In other words, Python automatically binds the first argument of the class method to the object calling the method. In this way, the Python interpreter knows exactly which object's method to manipulate.

As a result, the program does not need to manually pass a value for the first parameter when calling instance and constructor methods. For example, change the previous Person class as follows:

class Person:
def__init__(self):
print("Constructor being executed.")
# Define a study() instance method
defstudy(self):
print(self,"Learning Python.")
zhangsan =Person()
()
lisi =Person()
()

In the above code, self in study() represents the caller of the method, i.e., whoever calls the method. Therefore, the result of this program is:

Constructor method being executed
<__main__.Person object at 0x0000021ADD7D21D0> Learning Python now!
Constructor method being executed
<__main__.Person object at 0x0000021ADD7D2E48> Learning Python now!

In addition, the self argument to the constructor represents the class object that is currently being initialized. As an example:

class Person:
    name ="xxx"
def__init__(self,name):
        =name
zhangsan =Person("zhangsan")
print()
lisi =Person("lisi")
print()

The results of the run are:

zhangsan
lisi

You can see that zhangsan is initialized by calling a constructor where self represents zhangsan, while lisi is initialized by calling a constructor where self represents lisi.

It's worth noting that in addition to class objects being able to call class methods directly, there's also a way of calling functions, for example:

class Person:
defwho(self):
print(self)
zhangsan =Person()
# The first way
()
# The second way
who = 
who()#pass (a bill or inspection etc) who variable callzhangsanin an object who() methodologies

The results of the run are:

<__main__.Person object at 0x0000025C26F021D0>
<__main__.Person object at 0x0000025C26F021D0>

Obviously, no matter which method is used, self represents the object that actually calls the method.

In short, whether it's a class constructor or a regular class method, the first argument to self is whoever actually calls them.

to this article on the Python self parameter details of the article is introduced to this, more related Python self content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!