Home >Backend Development >Python Tutorial >Understanding self in python

Understanding self in python

hzc
hzcOriginal
2020-06-18 10:47:164408browse

Understanding self in python

Understanding of self in python:

1. Where does self appear?

First of all, self is in the method of the class. When calling this method, there is no need to assign a value to self. Python will automatically assign a value to it, and this value is an instance of the class - the object itself. You can also change self to another name such as seef, but it is not recommended because everyone is accustomed to writing self by default.

When writing code, the method system of the class directly has self, as follows:

Secondly, self is the default first parameter of the class method and It is necessary. If you don’t write it, an error will be reported.

2. What is the value of self?

The value of self is automatically assigned by Python, and the value is the object itself. For example: Class Dog, its instance is d, then when d calls the class method jian(arge1, arge2), Python will automatically convert to Dog.jian(d, arge1, arge2). The value of self is the instance d of Dog.

The following example prints the value of self. (intance in the printed result means instance)

Recommended tutorial: "Python Tutorial"

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

Statement:
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