python中的'self'太多了。这是这种语言的一大缺陷。 [英] Too many 'self' in python.That's a big flaw in this language.

查看:74
本文介绍了python中的'self'太多了。这是这种语言的一大缺陷。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

我正在使用Python。我发现一个实例变量必须

受限于自我,

例如:

class a:

def __init __(self):

self.aa = 10

def bb(self):

print self.aa#在c ++中查看.if ,我可以用aa改变那个

变量


这对编码很不方便,特别是当你有很多
$ b时$ b变量

如果你的方法需要10个变量,你必须输入self。 10次​​

这也使你的变量更长。

HI
I''m currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That''s a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.


>从我的观点来看,我认为这只能帮助python解释器
>From My point,I think this only help python interpreter to deside



在哪里寻找。

有没有人知道'如何让翻译找到实例名称

空格优先?

或者任何方式让程序员的生活更轻松?

where to look for.
Is there anyone know''s how to make the interpreter find instance name
space first?
Or any way to make programmer''s life easier?

推荐答案

在< 11 ********************* @ e9g2000prf.googlegroups.c om>,
hi ****** @ gmail.com 写道:
In <11*********************@e9g2000prf.googlegroups.c om>,
hi******@gmail.com wrote:

我目前正在使用Python。我发现一个实例变量必须

受限于自我,

例如:

class a:

def __init __(self):

self.aa = 10

def bb(self):

print self.aa#在c ++中查看.if ,我可以用aa改变那个

变量


这对编码很不方便,特别是当你有很多
$ b时$ b变量

如果你的方法需要10个变量,你必须输入self。 10次​​

这也使你的变量更长。
I''m currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That''s a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.

>>从我的观点来看,我认为这只能帮助python解释器to deside
>>From My point,I think this only help python interpreter to deside



在哪里寻找。

有没有人知道'如何让翻译找到实例名称
首先是
空间?

或者任何方式让程序员的生活更轻松?

where to look for.
Is there anyone know''s how to make the interpreter find instance name
space first?
Or any way to make programmer''s life easier?



使用比self更短的名称或自动完成的编辑器。如果某个函数或方法中的某个名称是本地的或全局的,则在编译时决定

时间,而不是在运行时。因此,至少对实例

属性的每个赋值必须在前面具有self。或者编译器将名称

视为该方法的本地。更改这会降低解释器的速度

,因为每次都必须在实例dict中查找每个名称

来判断它是属性还是本地名称。


你提出的魔法的另一个缺点是是在运行时动态分配,删除或委派属性是

。因此,你的裸aa`

名称可以改变从实例属性到本地名称或副本的含义

。反之亦然。


你必须有非常令人信服的理由要求进行更改,而顺便提一下。 Pythonistas通常不喜欢牺牲更少字符的
可读性。大多数源代码将被写入一次

但必须阅读并理解几次,所以更重要的是

清除短代码。有了`self`,你总能知道哪些是
名字是本地的,哪些是属性。


Ciao,

Marc' 'BlackJack''Rintsch

Use a shorter name than `self` or an editor with auto completion. If a
name in a function or method is local or global is decided at compile
time, not at run time. So at least every assignment to an instance
attribute must have the ``self.`` in front or the compiler sees the name
as local to the method. Changing this would slow down the interpreter
because every name has to be looked up in the instance dict every time to
decide if it''s an attribute or a local name.

Another drawback of your proposed "magic" is that attributes can be
assigned, deleted or delegated dynamically at run time. So your bare `aa`
name can change meaning from instance attribute to local name or vice
versa over the time.

You must have very compelling reasons to ask for changes that spare you
some keystrokes by the way. Pythonistas usually don''t like sacrificing
readability for fewer characters. Most source code will be written once
but must be read and understood a couple of times, so it''s more important
to have clear than short code. With `self` in place you always know which
names are local and which are attributes.

Ciao,
Marc ''BlackJack'' Rintsch


2007-06-27, hi ** ****@gmail.com < hi ****** @ gmail.comwrote:
On 2007-06-27, hi******@gmail.com <hi******@gmail.comwrote:

HI

我目前正在使用Python。我发现一个实例变量必须

受限于自我,

例如:

class a:

def __init __(self):

self.aa = 10

def bb(self):

print self.aa#在c ++中查看.if ,我可以用aa改变那个

变量


这对编码很不方便,特别是当你有

时很多变量如果你的方法需要10个变量,你必须

类型self 10次​​,这也使你的变量

更长。
HI
I''m currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That''s a big inconvenience in coding ,especially when you have
lot of variable If you method need 10 variables ,you have to
type "self" for 10 times and that also makes your variable
longer.



我建议在Python FAQ中讨论这个问题。

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


从我的观点来看,我认为这只能帮助python翻译来

deside在哪里寻找。是否有人知道如何使解释器首先找到实例名称空间?/ b
或者以任何方式获得
让程序员的生活变得更轻松?
From My point,I think this only help python interpreter to
deside where to look for. Is there anyone know''s how to make
the interpreter find instance name space first? Or any way to
make programmer''s life easier?



尝试考虑自我。作为一种符号,为程序员提供重要的

信息。


-

Neil Cerutti

Try thinking of "self." as a notation that provides vital
information to you, the programmer.

--
Neil Cerutti


6月27日上午7:02,hide1 ... @ gmail.com < hide1 ... @ gmail.comwrote:
On Jun 27, 7:02 am, "hide1...@gmail.com" <hide1...@gmail.comwrote:

HI

我正在使用Python。我发现一个实例变量必须

受限于自我,

例如:

class a:

def __init __(self):

self.aa = 10

def bb(self):

print self.aa#在c ++中查看.if ,我可以用aa改变那个

变量


这对编码很不方便,特别是当你有很多
$ b时$ b变量

如果你的方法需要10个变量,你必须输入self。 10次​​

这也使你的变量更长。
HI
I''m currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That''s a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.

从我的观点来看,我认为这只能帮助python解释器来预测
From My point,I think this only help python interpreter to deside



在哪里寻找。

有没有人知道如何让翻译找到实例名称

空间第一?

还是以任何方式让程序员的生活更轻松?


where to look for.
Is there anyone know''s how to make the interpreter find instance name
space first?
Or any way to make programmer''s life easier?

http:// www.voidspace.org.uk/python/w..._16.shtml#e584

http://www.voidspace.org.uk/python/w..._16.shtml#e584


这篇关于python中的'self'太多了。这是这种语言的一大缺陷。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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