为自己使用空字符串 [英] Use empty string for self
问题描述
似乎很多人不喜欢自我加注。在Python中编写方法时,实例变量是
。当然,每当有人建议取消自我时,很多人都会指出自己带来的范围优势。
。但是当我搜索时我没有看到这个
提案,所以我想我会把它扔出去。也许
它已被抛出但我喜欢它。
我自己的问题。是因为它使代码更大,而且比它需要的更复杂。特别是在数学表达式中:
self.position [0] = self.startx + len(self.bitlist)* self.bitwidth
它真的让代码难以阅读。另一方面,
消除了自我。会产生其他问题,包括可读性
关于哪些vars是实例变量,哪些来自
其他地方。
但是如果我们保留''''并省略自我怎么办?然后示例
看起来像:
..position [0] = .startx + len(.bitlist)* .bitwidth
自我是隐含的但是范围规则没有改变,并且在阅读它时它仍然是明确的,它们是实例变量。我们可以在方法标题中保留
,但这实际上是一个单独的
问题。
任意评论?之前是否讨论过这个问题?
pa ******** @ gmail.com 写道:似乎很多人不喜欢自我加注。在前面
....但是如果我们保留''''并且忽略自我呢?
....有什么意见吗?之前是否讨论过这个问题?
是的,至少一次(在这个
新闻组中通过group-googling找到删除自我) ):
http://groups.google.com/group/comp....791fdd4734579c
-Peter
谢谢。我以为肯定以前一定要讨论过,但无论如何,我的谷歌搜索技巧无法找到它。
< blockquote> pa********@gmail.com 写道:
有什么意见吗?之前是否讨论过这个问题?
是的。致死执行摘要:自我留在这里。
It seems that lots of people don''t like having to prefix self. in front
of instance variables when writing methods in Python. Of course,
whenever someone suggests doing away with ''self'' many people point to
the scoping advantages that self brings. But I hadn''t seen this
proposal when I searched so I thought I''d throw it out there. Maybe
it''s already been thrown out but I like it.
The issue I have with self. is that is makes the code larger and more
complicated than it needs to be. Especially in math expressions like:
self.position[0] = self.startx + len(self.bitlist) * self.bitwidth
It really makes the code harder to read. On the other hand,
eliminating the self. would create other issues including readability
with regards to which vars are instance vars and which come from
somewhere else.
But what if we keep the ''.'' and leave out the self. Then the example
looks like:
..position[0] = .startx + len(.bitlist) * .bitwidth
The ''self'' is implied but the scoping rules don''t change and it''s still
clear when reading it that they are instance variables. We can keep
the self in the method header (or not) but that is really a separate
issue.
Any comments? Has this been discussed before?
pa********@gmail.com wrote:It seems that lots of people don''t like having to prefix self. in front .... But what if we keep the ''.'' and leave out the self. .... Any comments? Has this been discussed before?
Yes, at least once (found by group-googling for "removing self" in this
newsgroup):
http://groups.google.com/group/comp....791fdd4734579c
-Peter
Thanks. I thought for sure it must have been discussed before but for
whatever reason, my googling skills couldn''t locate it.
pa********@gmail.com wrote:Any comments? Has this been discussed before?
Yes. To death. Executive summary: self is here to stay.
这篇关于为自己使用空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!