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的用法。 这通常是专家之间争论的焦点,也是初学者的困惑主题。 让我们为每个人做好准备。
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 constructor
def __init__(self, n):
self.name = n
def 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 constructor
def __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帖子,请阅读此处 。
python自我复制的程序