Home > Backend Development > Python Tutorial > python self what does it mean

python self what does it mean

(*-*)浩
Release: 2020-09-18 13:48:00
Original
37409 people have browsed it

self means "self", which in Python represents an instance of a class, not a class. Self can only be found in class methods. Independent functions or methods do not need to have self; self must be present when defining class methods. The name of self is not necessary. You can define it as a or b or any other name, but it is conventional and can reduce the difficulty of understanding.

python self what does it mean

First of all, we need to understand why the word self is used in Python classes. Why not use names like "zhangsan" and "lisi" Well, this must have his intention.

self represents an instance of a class, not a class.

class Test:
    def prt(self):
        print(self)
        print(self.__class__)
 
t = Test()
t.prt()
Copy after login

The execution results are as follows:

<__main__.Test object at 0x000000000284E080>
<class &#39;__main__.Test&#39;>
Copy after login

It is obvious from the above example that self represents an instance of the class. And self.class points to the class.

The first parameter of the method in the class must be self, otherwise the instance cannot correctly call the method in the class. That is to say, if the first parameter in the method is not self (in a broad sense), then this method is It has no value because the instance cannot call it. I really don’t know what use a method that cannot be called is.

Note: Replace self with this, the result will be the same, but it is best to use the conventional self in Python.

When inheriting, which instance is passed in is the instance passed in, not the instance of the class in which self is defined.

class Parent:
    def pprt(self):
        print(self)
 
class Child(Parent):
    def cprt(self):
        print(self)
c = Child()
c.cprt()
c.pprt()
p = Parent()
p.pprt()
Copy after login

The running results are as follows:

<__main__.Child object at 0x0000000002A47080>
<__main__.Child object at 0x0000000002A47080>
<__main__.Parent object at 0x0000000002A47240>
Copy after login

Explanation:

There should be no understanding problems when running c.cprt(), which refers to instances of the Child class.

But when running c.pprt(), it is equivalent to Child.pprt(c), so self still refers to an instance of the Child class. Since the pprt() method is not defined in self, inheritance is inherited Looking up the tree, we found that the pprt() method is defined in the parent class Parent, so it will be called successfully

The above is the detailed content of python self what does it mean. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template