为什么“自我”的论点? [英] Why the 'self' argument?

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

问题描述

您好,

我是新手Python用户,系统管理员 - 我一直在尝试将
从Perl切换到Python管理任务 - 还有一件事

到目前为止我无法理解为什么我需要在类方法定义中使用特殊的''self''(或任何

esle)参数。我可能错过了文档中的

解释,快速的Google搜索没有帮助。你可以在网上的某个地方找到

吗?


谢谢,


-

Grzegorz Staniak< gs ****** @ zagiel.com.pl>

Hello,

I''m a newbie Python user, a systems administrator - I''ve been trying
to switch from Perl to Python for administrative tasks - and one thing
I cannot understand so far is why I need the special ''self'' (or anything
esle) argument in class method definitions. I might have missed an
explanation in the docs, a quick Google search did not help. Is there
somewhere on the web you could direct me to?

Thanks,

--
Grzegorz Staniak <gs******@zagiel.com.pl>

推荐答案

在文章< vl ************@news.supernews.com> ;, John Roth写道:
In article <vl************@news.supernews.com>, John Roth wrote:
从技术上讲,可以制造自我。一个保留的单词,而不必把它放在方法声明中。
然而,有很多人使用的不是自我这个词。因此会破坏现有代码。
Technically, it would be possible to make "self" a reserved
word, and not have to put it in the method declaration.
However, there are a lot of people who use something other than
the word "self," so that would break existing code.




这也会使语言变得更加复杂和不规则。


-

格兰特爱德华兹格兰特哇!我可以服用过量药物

吗?

visi.com



It would also make the language more complex and irregular.

--
Grant Edwards grante Yow! Could I have a drug
at overdose?
visi.com


Grzegorz Staniak写道:
Grzegorz Staniak wrote:
您好,

我是新手Python用户,系统管理员 - 我一直在尝试从Perl切换到Python以执行管理任务 - 还有一件事<到目前为止,我无法理解为什么我需要在类方法定义中使用特殊的自我(或任何
esle)参数。我可能错过了文档中的
解释,快速的Google搜索没有帮助。你可以在网上的某个地方指引我吗?
Hello,

I''m a newbie Python user, a systems administrator - I''ve been trying
to switch from Perl to Python for administrative tasks - and one thing
I cannot understand so far is why I need the special ''self'' (or anything
esle) argument in class method definitions. I might have missed an
explanation in the docs, a quick Google search did not help. Is there
somewhere on the web you could direct me to?




Python常见问题解答的这一条:

http://www.python.org/doc/faq/genera...ions-and-calls

- Gerhard



This entry of the Python FAQ:

http://www.python.org/doc/faq/genera...ions-and-calls

-- Gerhard


2003年9月5日星期五下午01:58:41 +0000,Grzegorz Staniak写道:
On Fri, Sep 05, 2003 at 01:58:41PM +0000, Grzegorz Staniak wrote:
你好,
到目前为止我无法理解为什么我需要类方法定义中的特殊自我(或任何
esle)参数。我可能错过了文档中的
解释,快速的Google搜索没有帮助。网上有什么地方可以指示我吗?

谢谢,

-
Grzegorz Staniak< gs ***** *@zagiel.com.pl>
-
http://mail.python.org/mailman/listinfo/python-list




类方法中的第一个参数是对那个班级本身。与这个相同的是&b
c ++和java中的指针。使用此参数,您可以访问

类变量。例如:


类测试:

def __init __(self,arg1,arg2):#this充当类构造函数

self .name = arg1

self.value = arg2

def changeName(self,newName):#我们更改名称类变量

self .name = newName

..........等等


问候

Vivek Kumar



The first argument in a class method is a reference to that class itself. It is
same as "this" pointer in c++ and java. Using this argument you can access the
class variables. ex:

class test:
def __init__(self,arg1,arg2): #this acts as class constructor
self.name=arg1
self.value=arg2
def changeName(self,newName): #here we change the name class variable
self.name=newName
..........and so on

regards
Vivek Kumar


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

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