python自我复制的程序_Python自我

本文探讨了Python中的self关键字,它不是Python的保留关键字,但作为类方法中的第一个参数,用于引用对象实例。文章解释了如何在类构造函数中使用self,以及为何在方法调用中传递self。此外,还讨论了self是否必须作为参数,以及其在对象引用中的作用。
摘要由CSDN通过智能技术生成

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帖子,请阅读此处

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

python自我复制的程序

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值