明确的自我重访 [英] explicit self revisited

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

问题描述

Python FAQ 1.4.5给出了显式自我(浓缩版)的3个理由:


1.实例变量可以很容易地与局部变量区分开来。


2.来自特定类的方法可以被称为

baseclass.methodname(self,< argument list>)。


3.无需声明来消除对本地/实例

变量的赋值的歧义。


所有这些原因都是有效的并且由以下建议保留:let

自我由点表示,例如替换


class someTest(unittest.TestCase):

def setUp(self):

self.ly = yList()

self.m1 = self.ly [0] .message

self.m2 = self.ly [1] .message

self。 m3 = self.ly [2] .message


def testList(self):

self.assertEqual(len(self.ly),3)

self.assertEqual(self.m1),Ho)

self.assertEqual(self.m2),HoHo)

self.assertEqual(self.m3),HoHoHo)





class x(unittest.TestCase):

def .setUp():

.ly = yList()

.m1 = .ly [0] .message

.m2 = .ly [1] .message

.m3 = .ly [2] .message


def .testList():

.assertEqual(len(.ly),3)

.assertEqual(.m1)," Ho")

.assertEqual(.m2 ),HoHo)

.assertEqual(.m3),HoHoHo)


方法仍可参考,例如as x.testList(someInstance)。

当前的自语法仍然有效(为了向后兼容。)

新语法的优点:


1.增强可读性,减少冗长


2.毫不含糊:无需告诉新手一个贤惠的pythoneer

必须坚持自我而不是将其缩写为s。


3.对于Python OO boled on而言,宣传。


第二个原因对我来说最重要。我认为语法控制

的行为准则是​​蹩脚的。


领先点语法还有其他优点:


class x(object):

a ="" #static变量

.b = 0#instance variable


这可以替换无参数的__init__方法。如果没有静态方法装饰器,没有引导
点的方法可以被认为是静态的。对于

向后兼容性,应明确激活此行为,例如by

__autostatic__ = true。


您怎么看?


-

问候/ Gruesse,

Peter Maas,Aachen

电子邮件''cGV0ZXIubWFhc0B1dGlsb2cuZGU = \ n''。decode(''base64'' )

解决方案

Peter Maas写道:


您怎么看?



你能不能把所有无法想到一个有用的东西的无知巨魔

贡献给Python开始你自己的新闻组?


< / F>



不是所有你无能为力的巨魔谁想不出一个有用的东西

有助于Python创建自己的新闻组吗?



并且在任何人投诉之前;请注意他们正在通过

http://www.effbot.org/pyfaq/design-index.htm


一次一篇文章。谁将成为第一个认为

Python需要goto声明的人?


< / F>




Fredrik Lundh写道:


Fredrik Lundh写道:
< blockquote class =post_quotes>
不能让所有你无能为力的巨魔们想到一个有用的东西

贡献给Python开始你自己的新闻组?



在任何人抱怨之前;请注意他们正在通过

http://www.effbot.org/pyfaq/design-index.htm



该网站是一堆FUD -

显而易见的自我只是因为OOP被添加到python上,因为

是事后的想法。

为什么不诚实。改变Python的

语法已经太晚了。这只是意味着一点额外打字。如果真的困扰某人,请使用s。而不是自我或者使用Ruby。


The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version):

1. Instance variables can be easily distinguished from local variables.

2. A method from a particular class can be called as
baseclass.methodname(self, <argument list>).

3. No need for declarations to disambiguate assignments to local/instance
variables.

All these reasons are valid and retained by the following suggestion: let
self be represented by the dot, e.g. replace

class someTest(unittest.TestCase):
def setUp(self):
self.ly = yList()
self.m1 = self.ly[0].message
self.m2 = self.ly[1].message
self.m3 = self.ly[2].message

def testList(self):
self.assertEqual(len(self.ly),3)
self.assertEqual(self.m1),"Ho")
self.assertEqual(self.m2),"HoHo")
self.assertEqual(self.m3),"HoHoHo")

by

class x(unittest.TestCase):
def .setUp():
.ly = yList()
.m1 = .ly[0].message
.m2 = .ly[1].message
.m3 = .ly[2].message

def .testList():
.assertEqual(len(.ly),3)
.assertEqual(.m1),"Ho")
.assertEqual(.m2),"HoHo")
.assertEqual(.m3),"HoHoHo")

Methods could still be referenced e.g. as x.testList(someInstance).
The current self syntax could still be valid (for backward compatibility.)
Advantages of the new syntax:

1. Enhanced readability, less verbosity

2. Unambiguous: no need to tell newbies that a virtuous pythoneer
has to stick to self instead of abbreviate it as s.

3. One argument less for "Python OO bolted on" propaganda.

The second reason is the most important for me. I consider syntax control
by a code of conduct as lame.

The "leading dot" syntax could have further advantages:

class x(object):
a = "" # static variable
.b = 0 # instance variable

This could replace parameterless __init__ methods. Methods without leading
dots could be considered static without a staticmethod decorator. For
backward compatibility this behaviour should be explicitly activated e.g. by
__autostatic__ = true.

What do you think?

--
Regards/Gruesse,

Peter Maas, Aachen
E-mail ''cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n''.decode(''base64'')

解决方案

Peter Maas wrote:

What do you think?

cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?

</F>


cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?

and before anyone complains; please note that they''re working through

http://www.effbot.org/pyfaq/design-index.htm

one article at a time. who''s going to be the first one to argue that
Python needs a goto statement ?

</F>



Fredrik Lundh wrote:

Fredrik Lundh wrote:

cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?


and before anyone complains; please note that they''re working through

http://www.effbot.org/pyfaq/design-index.htm

That site is a bunch of FUD -
The explicit self is there simply because OOP was tacked onto python as
an afterthought.
Why not just be honest about it. It''s too late to change Python''s
syntax. It just means a little extra typing. If it really bothers
someone, use "s" instead of "self" or else use Ruby.


这篇关于明确的自我重访的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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