本文介绍了明确的自我重访的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 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'' ) 解决方案 你能不能把所有无法想到一个有用的东西的无知巨魔 贡献给Python开始你自己的新闻组? < / F> 并且在任何人投诉之前;请注意他们正在通过 http://www.effbot.org/pyfaq/design-index.htm 一次一篇文章。谁将成为第一个认为 Python需要goto声明的人? < / F> 在任何人抱怨之前;请注意他们正在通过 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 asbaseclass.methodname(self, <argument list>).3. No need for declarations to disambiguate assignments to local/instancevariables.All these reasons are valid and retained by the following suggestion: letself be represented by the dot, e.g. replaceclass someTest(unittest.TestCase):def setUp(self):self.ly = yList()self.m1 = self.ly[0].messageself.m2 = self.ly[1].messageself.m3 = self.ly[2].messagedef testList(self):self.assertEqual(len(self.ly),3)self.assertEqual(self.m1),"Ho")self.assertEqual(self.m2),"HoHo")self.assertEqual(self.m3),"HoHoHo")byclass x(unittest.TestCase):def .setUp():.ly = yList().m1 = .ly[0].message.m2 = .ly[1].message.m3 = .ly[2].messagedef .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 verbosity2. Unambiguous: no need to tell newbies that a virtuous pythoneerhas 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 controlby a code of conduct as lame.The "leading dot" syntax could have further advantages:class x(object):a = "" # static variable.b = 0 # instance variableThis could replace parameterless __init__ methods. Methods without leadingdots could be considered static without a staticmethod decorator. Forbackward compatibility this behaviour should be explicitly activated e.g. by__autostatic__ = true.What do you think?--Regards/Gruesse,Peter Maas, AachenE-mail ''cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n''.decode(''base64'') 解决方案cannot all you clueless trolls who cannot think of a single useful thingto contribute to Python start your own newsgroup?</F>and before anyone complains; please note that they''re working through http://www.effbot.org/pyfaq/design-index.htmone article at a time. who''s going to be the first one to argue thatPython needs a goto statement ?</F>and before anyone complains; please note that they''re working through http://www.effbot.org/pyfaq/design-index.htmThat site is a bunch of FUD -The explicit self is there simply because OOP was tacked onto python asan afterthought.Why not just be honest about it. It''s too late to change Python''ssyntax. It just means a little extra typing. If it really botherssomeone, use "s" instead of "self" or else use Ruby. 这篇关于明确的自我重访的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 17:01