什么是“自我”? [英] What is "self"?

查看:93
本文介绍了什么是“自我”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我是新手...

我正在努力学习Python&到目前为止,我们玩得很开心。但是我跟着对象self的许多代码示例有麻烦。可以

有人用简单的英语解释这个用法吗?


谢谢,

Wayne

OK, I''m a newbie...
I''m trying to learn Python & have had fun with it so far. But I''m having
trouble following the many code examples with the object "self." Can
someone explain this usage in plain english?

Thanks,
Wayne

推荐答案

self是被调用的绑定函数属于

的类实例。这个例子应该说明一下。

类Foo(对象):

def __init __(自我,值):

self.value = value #所以Foo实例现在有一个属性,




def get_value(个体经营):

返回self.value#这得到了Foo实例之前设定的值

属性


bar = Foo(42)

baz = Foo( ''101010'')


print bar.get_value()#注意self参数是隐含的,因为

这是一个绑定方法。

打印

print baz.get_value()


输出是(或者应该是,因为这是未经测试的):

42

101010

self is the class instance that the bound function being called belongs
to. This example should illustrate a bit.
class Foo(object):
def __init__(self, value):
self.value = value # so the Foo instance now has an attribute,
value

def get_value(self):
return self.value # This gets the previously-set value
attribute of the Foo instance

bar = Foo(42)
baz = Foo(''101010'')

print bar.get_value() #Note that the self argument is implicit since
this is a bound method.
print
print baz.get_value()

The output is (or should be, as this is untested):
42

101010


On Thu,2005-09-22 at 21:36 - 0400,Wayne Sutton写道:
On Thu, 2005-09-22 at 21:36 -0400, Wayne Sutton wrote:
好的,我是新手...
我正在努力学习Python&到目前为止,我们玩得很开心。但是我在使用对象self跟踪许多代码示例时遇到了麻烦。有人可以用简单的英语解释这种用法吗?
OK, I''m a newbie...
I''m trying to learn Python & have had fun with it so far. But I''m having
trouble following the many code examples with the object "self." Can
someone explain this usage in plain english?




" self"引用对象本身。它通常也被称为这个。 on

其他语言(C ++& Java我相信)。在Python中,当你定义一个

类方法时,对该对象的引用明确地传递而不是隐式地传递给




此外,名称自我是指自我。按惯例使用。您可以使用任何名称,如果您想要

,但如果您希望其他人理解您的代码,那么

请使用self。


那是否足够简单?



"self" references the object itself. It''s usually also called "this" on
other languages (C++ & Java I believe). In Python, when you define a
class method, the reference to the object is passed explicitly rather
than implicitly.

Also, the name "self" is used by convention. You could use any name if
you wanted, but if you want other people to understand your code then
use "self".

Is that plain English enough?


Wayne Sutton写道:
Wayne Sutton wrote:
好的,我是新手.. 。
我正在努力学习Python&到目前为止,我们玩得很开心。但是我在使用对象self跟踪许多代码示例时遇到了麻烦。有人可以用简单的英语解释这个用法吗?

谢谢,
Wayne
OK, I''m a newbie...
I''m trying to learn Python & have had fun with it so far. But I''m having
trouble following the many code examples with the object "self." Can
someone explain this usage in plain english?

Thanks,
Wayne



我会试一试..


当你有一个班级定义:


class Person(object):

def set_name(self,名称):

self.name = name


方法set_name,不知道你的类实例会是什么

此时调用。但它需要知道什么时候被调用。所以

现在自我只是一个等待被赋予引用的参数

,就像任何其他函数参数一样。你实际上可以称之为

你想要的任何东西,但是自我是一种传统。


leader = Person()

这会创建一个类的实例并在

名称为leader。既然你有一个名字的实例。你

可以用你的班级方法做点什么。


leader.set_name(" John")


当你调用一个实例的方法时,Python将它转换为......


leader.set_name(leader,John)


所以自我在你的方法定义中被赋予对

leader的引用。 "自"然后可以用来访问存储在类实例中的其他值和

方法。或者在这种情况下,将一个值存储在您的类实例的
中。基本上是自我成为班级的参考

实例。


self.name = name


是同样如...


leader.name = name


但我们并不知道它会被称为领导者 ;当我们写了

类。所以自我是一个方便的占位符。

我希望这有帮助。


干杯,

Ron




I''ll give it a try..

When you have a class definition:

class Person(object):
def set_name(self, name):
self.name = name

The method "set_name", has no idea what your class instance is going to
called at this point. But it will need to know when it''s called. So
for now "self" is just a argument waiting to be assigned a reference
later just like any other function argument. You can actually call it
anything you want but "self" is sort of a tradition.

leader = Person()

This creates an instance of your class and stores a reference to it in
the name "leader". Now that that you have an instance with a name. You
can use your class method to do something.

leader.set_name("John")

When you call a method of an instance, Python translates it to...

leader.set_name(leader, "John")

So "self" in your method definition gets assigned a reference to
"leader". "self" can then be used to access the other values and
methods stored in your class instance. Or in this case store a value in
your class instance. Basically "self" becomes a reference to the class
instance it is in.

self.name = name

is the same as ...

leader.name = name

But we didn''t know it was going to be called "leader" when we wrote the
class. So self is a convienent place holder.
I hope this helped.

Cheers,
Ron




这篇关于什么是“自我”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆