覆盖的方法不包含自我?(Overwritten Methods don't contain self?)

编程入门 行业动态 更新时间:2024-10-24 22:27:03
覆盖的方法不包含自我?(Overwritten Methods don't contain self?)

这是我刚刚在我的机器上播放的一个例子:

$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. # just a test class >>> class A(object): ... def hi(self): ... print("hi") ... >>> a = A() >>> a.hi() hi >>> def hello(self): ... print("hello") ... >>> >>> hello(None) hello >>> >>> >>> >>> a.hi = hello # now I would expect for hi to work the same way as before # and it just prints hello instead of hi. >>> a.hi() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: hello() takes exactly 1 argument (0 given) >>> >>> def hello(): ... print("hello") ... # but instead this one works, which doesn't contain any # reference to self >>> a.hi = hello >>> a.hi() hello >>> >>> >>> >>> >>> a.hello = hello >>> a.hello() hello

这里发生了什么? 当它用作方法时,为什么函数不会获得参数self? 我需要做些什么来获得对自我的引用?

Here is an example I just played through on my machine:

$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. # just a test class >>> class A(object): ... def hi(self): ... print("hi") ... >>> a = A() >>> a.hi() hi >>> def hello(self): ... print("hello") ... >>> >>> hello(None) hello >>> >>> >>> >>> a.hi = hello # now I would expect for hi to work the same way as before # and it just prints hello instead of hi. >>> a.hi() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: hello() takes exactly 1 argument (0 given) >>> >>> def hello(): ... print("hello") ... # but instead this one works, which doesn't contain any # reference to self >>> a.hi = hello >>> a.hi() hello >>> >>> >>> >>> >>> a.hello = hello >>> a.hello() hello

What is happening here? Why doesn't the function get the parameter self, when it is used as a method? What would I need to do, to get a reference to self inside it?

最满意答案

通过实例引用的类中的方法绑定到该实例,在您的情况下:

In [3]: a.hi Out[3]: <bound method A.hi of <__main__.A object at 0x218ab10>>

相比于:

In [4]: A.hi Out[4]: <unbound method A.hi>

所以,要达到你可能想要的效果,请这样做

In [5]: def hello(self): ...: print "hello" ...: In [6]: A.hi = hello In [7]: a.hi() hello

注意 - 这将适用于A所有实例。 但是,如果你只想在一个实例上覆盖一个方法,你是否真的需要通过self ?

A method in a class referenced via an instance is bound to that instance, in your case:

In [3]: a.hi Out[3]: <bound method A.hi of <__main__.A object at 0x218ab10>>

Compare to:

In [4]: A.hi Out[4]: <unbound method A.hi>

So, to achieve the effect you probably want, do

In [5]: def hello(self): ...: print "hello" ...: In [6]: A.hi = hello In [7]: a.hi() hello

Beware - this will apply to all instances of A. But if you want to override a method on one instance only, do you really need to pass self?

更多推荐

本文发布于:2023-04-27 20:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329070.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包含   自我   方法   Methods   Overwritten

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!