Home Backend Development Python Tutorial How to use self in Python

How to use self in Python

May 17, 2023 pm 10:40 PM
python self

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python
We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates. For example, an abstract thing like a student can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different.
1. Take the Student class as an example. In Python, the class is defined as follows:

class Student(object):
    pass
Copy after login

(Object) indicates which class the class inherits from. The Object class is a class that all classes will inherit.

2. Instance: After the class is defined, an instance of Student can be created through the Student class. The instance is created through the class name ():

student = Student()
Copy after login

3. Since the class serves as a template Therefore, when creating an instance, we can forcefully fill in the attributes that we think must be bound. A built-in method in Python, the __init__ method, is used here. For example, in the Student class, tie attributes such as name and score to it:

class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score
Copy after login

Note here: (1), ## The first parameter of the #__init__ method is always self, which represents the created class instance itself. Therefore, within the __init__ method, you can put each This property is bound to self, because self points to the created instance itself. (2). With the __init__ method, when creating an instance, you cannot pass in empty parameters. You must pass in parameters that match the __init__ method, but self is not required. Pass, the Python interpreter will pass the instance variables in by itself:

>>>student = Student("Hugh", 99)
>>>student.name
"Hugh"
>>>student.score
99
Copy after login

In addition, here

self refers to the class itself, self.name is Student# The attribute variables of the ## class are owned by the Student class. The name is an external parameter, not one that comes with the Student class. Therefore, self.name = name means to assign the value of the external parameter name to the Student class’s own attribute variable self.name. 4. Compared with ordinary numbers, there is only one difference when defining a function in a class, that is,

The first parameter

is always the instance variable of the class itselfself , and when calling, there is no need to pass this parameter. In addition, class methods (functions) are no different from ordinary functions. You can use default parameters, variable parameters or keyword parameters (*args is variable parameters, args What is received is a tuple, **kw is a keyword parameter, and what kw receives is a dict). 5. Since the

Student

class instance itself owns these data, then to access these data, there is no need to access it from external functions, but you can directly define access within the Student class. Functions (methods) of data, so that "data" can be encapsulated. These functions that encapsulate data are associated with the Student class itself, and are called class methods:

class Student(obiect):
    def __init__(self, name, score):
        self.name = name
        self.score = score
    def print_score(self):
        print "%s: %s" % (self.name, self.score)
Copy after login
>>>student = Student("Hugh", 99)
>>>student.print_score
Hugh: 99
Copy after login
In this way, when we look at the Student class from the outside, we only need to know what needs to be given to create an instance. name and score. How to print is defined inside the Student class.

These data and logic are encapsulated, making it easy to call, but the details of the internal implementation are not known.

If you want to prevent internal attributes from being accessed externally, you can add two underscores

before the name of the attribute. In Python, if the variable name of an instance starts with

, It has become a private variable (private), which can only be accessed internally and not externally. Therefore, we changed the Student class:

class Student(object):

    def __init__(self, name, score):
        self.__name = name
        self.__score = score
    def print_score(self):
        print "%s: %s" %(self.__name,self.__score)
Copy after login
After the change, there is no change for the external code, but Instance variable

.__name

and instance variable .__score can no longer be accessed from the outside: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>&gt;&gt;&gt; student = Student(&amp;#39;Hugh&amp;#39;, 99) &gt;&gt;&gt; student.__name Traceback (most recent call last): File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt; AttributeError: &amp;#39;Student&amp;#39; object has no attribute &amp;#39;__name&amp;#39;</pre><div class="contentsignin">Copy after login</div></div> This ensures that external code cannot modify the internal state of the object at will, so Code is more robust through the protection of access restrictions.

But what if the external code wants to get the name and score? You can add methods like get_name and get_score to the Student class:

class Student(object):
    ...

    def get_name(self):
        return self.__name

    def get_score(self):
        return self.__score
Copy after login

What if you want to allow external code to modify the score? You can add the set_score method to the Student class:

class Student(object):
    ...

    def set_score(self, score):
        self.__score = score
Copy after login

It should be noted that in Python, variable names are similar to

__xxx__

, that is, they start with a double underscore and end with a double underscore. It is a special variable. Special variables can be accessed directly and are not private variables. Therefore, variable names such as __name__ and __score__ cannot be used. Sometimes, you will see instance variable names starting with an underscore, such as _name. Such instance variables can be accessed externally. However, according to the convention, when you see such a variable When, it means, "Although I can be accessed, please treat me as a private variable and do not access it at will."

Another benefit of encapsulation is that you can add new methods

to the Student class at any time, such as: get_grade:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>class Student(object): ... def get_grade(self): if self.score &gt;= 90: return &amp;#39;A&amp;#39; elif self.score &gt;= 60: return &amp;#39;B&amp;#39; else: return &amp;#39;C&amp;#39;</pre><div class="contentsignin">Copy after login</div></div>Similarly, ## The #get_grade

method can be called directly on the instance variable without knowing the internal implementation details:

>>> student.get_grade()
&#39;A&#39;
Copy after login

6、self的仔细用法
(1)、self代表类的实例,而非类。

class Test:
    def ppr(self):
        print(self)
        print(self.__class__)

t = Test()
t.ppr()
执行结果:
<__main__.Test object at 0x000000000284E080>
<class &#39;__main__.Test&#39;>
Copy after login

从上面的例子中可以很明显的看出,self代表的是类的实例。而self.__class__则指向类。
注意:把self换成this,结果也一样,但Python中最好用约定俗成的self。
(2)、self可以不写吗?
在Python解释器的内部,当我们调用t.ppr()时,实际上Python解释成Test.ppr(t),也就是把self替换成了类的实例。

class Test:
    def ppr():
        print(self)

t = Test()
t.ppr()
Copy after login

运行结果如下:

Traceback (most recent call last):
File "cl.py", line 6, in
t.ppr()
TypeError: ppr() takes 0 positional arguments but 1 was given

运行时提醒错误如下:ppr在定义时没有参数,但是我们运行时强行传了一个参数。

由于上面解释过了t.ppr()等同于Test.ppr(t),所以程序提醒我们多传了一个参数t。

这里实际上已经部分说明了self在定义时不可以省略。

当然,如果我们的定义和调用时均不传类实例是可以的,这就是类方法。

class Test:
    def ppr():
        print(__class__)

Test.ppr()

运行结果:
<class &#39;__main__.Test&#39;>
Copy after login

(3)、在继承时,传入的是哪个实例,就是那个传入的实例,而不是指定义了self的类的实例。

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

运行结果:

<__main__.Child object at 0x0000000002A47080>
<__main__.Child object at 0x0000000002A47080>
<__main__.Parent object at 0x0000000002A47240>

解释:
运行c.cprt()时应该没有理解问题,指的是Child类的实例。
但是在运行c.pprt()时,等同于Child.pprt(c),所以self指的依然是Child类的实例,由于self中没有定义pprt()方法,所以沿着继承树往上找,发现在父类Parent中定义了pprt()方法,所以就会成功调用。

(4)、在描述符类中,self指的是描述符类的实例

class Desc:
    def __get__(self, ins, cls):
        print(&#39;self in Desc: %s &#39; % self )
        print(self, ins, cls)
class Test:
    x = Desc()
    def prt(self):
        print(&#39;self in Test: %s&#39; % self)
t = Test()
t.prt()
t.x
Copy after login

运行结果如下:

self in Test: <__main__.Test object at 0x0000000002A570B8>
self in Desc: <__main__.Desc object at 0x000000000283E208>
<__main__.Desc object at 0x000000000283E208> <__main__.Test object at 0x0000000002A570B8>

这里主要的疑问应该在:Desc类中定义的self不是应该是调用它的实例t吗?怎么变成了Desc类的实例了呢?
因为这里调用的是t.x,也就是说是Test类的实例t的属性x,由于实例t中并没有定义属性x,所以找到了类属性x,而该属性是描述符属性,为Desc类的实例而已,所以此处并没有顶用Test的任何方法。

那么我们如果直接通过类来调用属性x也可以得到相同的结果。

下面是把t.x改为Test.x运行的结果。

self in Test: <__main__.Test object at 0x00000000022570B8>
self in Desc: <__main__.Desc object at 0x000000000223E208>
<__main__.Desc object at 0x000000000223E208> None <class &#39;__main__.Test&#39;>
Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download DeepSeek Xiaomi? Search for "DeepSeek" in the Xiaomi App Store. If it is not found, continue to step 2. Identify your needs (search files, data analysis), and find the corresponding tools (such as file managers, data analysis software) that include DeepSeek functions.

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

The key to using DeepSeek effectively is to ask questions clearly: express the questions directly and specifically. Provide specific details and background information. For complex inquiries, multiple angles and refute opinions are included. Focus on specific aspects, such as performance bottlenecks in code. Keep a critical thinking about the answers you get and make judgments based on your expertise.

How to search deepseek How to search deepseek Feb 19, 2025 pm 05:18 PM

Just use the search function that comes with DeepSeek. Its powerful semantic analysis algorithm can accurately understand the search intention and provide relevant information. However, for searches that are unpopular, latest information or problems that need to be considered, it is necessary to adjust keywords or use more specific descriptions, combine them with other real-time information sources, and understand that DeepSeek is just a tool that requires active, clear and refined search strategies.

How to program deepseek How to program deepseek Feb 19, 2025 pm 05:36 PM

DeepSeek is not a programming language, but a deep search concept. Implementing DeepSeek requires selection based on existing languages. For different application scenarios, it is necessary to choose the appropriate language and algorithms, and combine machine learning technology. Code quality, maintainability, and testing are crucial. Only by choosing the right programming language, algorithms and tools according to your needs and writing high-quality code can DeepSeek be successfully implemented.

How to use deepseek to settle accounts How to use deepseek to settle accounts Feb 19, 2025 pm 04:36 PM

Question: Is DeepSeek available for accounting? Answer: No, it is a data mining and analysis tool that can be used to analyze financial data, but it does not have the accounting record and report generation functions of accounting software. Using DeepSeek to analyze financial data requires writing code to process data with knowledge of data structures, algorithms, and DeepSeek APIs to consider potential problems (e.g. programming knowledge, learning curves, data quality)

How to access DeepSeekapi - DeepSeekapi access call tutorial How to access DeepSeekapi - DeepSeekapi access call tutorial Mar 12, 2025 pm 12:24 PM

Detailed explanation of DeepSeekAPI access and call: Quick Start Guide This article will guide you in detail how to access and call DeepSeekAPI, helping you easily use powerful AI models. Step 1: Get the API key to access the DeepSeek official website and click on the "Open Platform" in the upper right corner. You will get a certain number of free tokens (used to measure API usage). In the menu on the left, click "APIKeys" and then click "Create APIkey". Name your APIkey (for example, "test") and copy the generated key right away. Be sure to save this key properly, as it will only be displayed once

Major update of Pi Coin: Pi Bank is coming! Major update of Pi Coin: Pi Bank is coming! Mar 03, 2025 pm 06:18 PM

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies

What are the current AI slicing tools? What are the current AI slicing tools? Nov 29, 2024 am 10:40 AM

Here are some popular AI slicing tools: TensorFlow DataSetPyTorch DataLoaderDaskCuPyscikit-imageOpenCVKeras ImageDataGenerator

See all articles