python 淺拷貝,python自我復制的程序_Python自我

 2023-11-19 阅读 32 评论 0

摘要:python自我復制的程序python 淺拷貝、In this lesson, we will study about the usage of Python self. It is often a point of debate among experts and a topic of confusion for beginners. Let’s try to come clean for everyone. 在本課程中,我們將研究Python

python自我復制的程序

python 淺拷貝、In this lesson, we will study about the usage of Python self. It is often a point of debate among experts and a topic of confusion for beginners. Let’s try to come clean for everyone.

在本課程中,我們將研究Python self的用法。 這通常是專家之間爭論的焦點,也是初學者的困惑主題。 讓我們為每個人做好準備。

python小程序。Now, this post is not meant to be an Introduction to Python but expects a very little understanding of Python syntax. For more posts on Python, see here.

現在,這篇文章并不是要成為Python入門,而是希望對Python語法有一點了解。 有關Python的更多信息,請參見此處 。

Python自我 (Python self)

Python is not a language which was made for Object-Oriented Programming paradigm in mind. So, it is not direct to create a static method in Python. Let’s see how this is done:

Python不是一種為面向對象編程范例而設計的語言。 因此, 在Python中創建靜態方法不是直接的。 讓我們看看如何做到這一點:

class Person:def static_method():print("This is a static method")

Now, to make a method which can operate on a real ‘Person’ object we need to provide it with a reference to the object. So, instead of passing the complete object of Person to its own class, we can use the self as:

現在,要制作一種可以在真實的“ Person”對象上運行的方法,我們需要為其提供對該對象的引用。 因此,我們可以將self用作:

class Person:def object_method(self):print("I can do something with self.")

Next, let us look at how it can be used to access fields in the class constructor itself.

接下來,讓我們看看如何使用它來訪問類構造函數本身中的字段。

Python類自構造函數 (Python class self constructor)

Python self can also be used to refer to a variable field within the class:

Python self也可以用于引用類中的變量字段:

class Person:# name made in constructordef __init__(self, n):self.name = ndef get_person_name(self):return self.name

In above snippet, self refers to the name variable of the entire Person class. Note that if we have a variable within a method, self will not work. That variable is simply existent only while that method is running and hence, is local to that method. For defining global fields (the variables of the complete class), we need to define them OUTSIDE the class methods. Refer python variable scope.

在上面的代碼片段中, self指整個Person類的名稱變量。 請注意, 如果我們在方法內有一個變量,則self將不起作用 。 該變量僅在該方法運行時才存在,因此是該方法的局部變量。 為了定義全局字段(完整類的變量),我們需要在類方法之外定義它們。 請參閱python變量scope 。

自我是關鍵字嗎? (Is self a keyword?)

self is used in so many places in Python that people think it’s a keyword. But unlike this in C++, self is not a keyword.

self在Python中的很多地方都使用過,人們以為它是一個關鍵字。 但是,與this在C ++中, self不是關鍵字

We can actually use any name for the first parameter of a method, but it is strongly advised to stick to the convention of calling it self.

實際上,我們可以將任何名稱用作方法的第一個參數,但強烈建議您遵循將其稱為self的約定。

This means that the last class can be made as:

這意味著最后一個類可以做成:

class Person:#name made in constructordef __init__(another, n):another.name = n;def get_person_name(another):return another.name

See how I used another this time? It works exactly the same way as self.

看到我這次如何使用another嗎? 它的工作方式與self完全相同。

我們應該將自己傳遞給方法嗎? (Should we pass self to a method?)

Above explanation opens a new question, should we just pass self to a method? Let’s consider the class Person which contains a method something defined as:

上面的解釋提出了一個新的問題,我們是否應該將self傳遞給方法? 讓我們考慮Person類包含一個方法something定義為:

def something(another, arg1, arg2):pass

If personMe is an instance of the class and personMe.something(arg1, arg2) is called, python internally converts it for us as:

如果personMe是該類的實例,并且personMe.something(arg1, arg2) ,則python在內部將其轉換為:

Person.something(personMe, arg1, arg2)

The self variable refers to the object itself.

self變量是指對象本身。

That’s all for python self and it’s usage in constructor and functions to get the current object reference.

這就是python self的全部內容,并且在構造函數和函數中用于獲取當前對象引用。

For more Python posts, read here.

有關更多Python帖子,請閱讀此處 。

翻譯自: https://www.journaldev.com/17278/python-self

python自我復制的程序

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/182969.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息