Python新手问个调用成员函数的问题

2013-01-02 18:37:39 +08:00
 xsaps
在父函数中调用子函数的成员函数, 遇到个问题, 代码如下

class Foo(object):
def __init__(self):
self._func = None

def callDerivedFunc(self):
# self._func() # wrong
method = self._func
method(self) # right

class Bar(Foo):
def __init__(self):
super(Foo, self).__init__()
self._func = Bar.hello

def hello(self):
print 'Hello'

bar = Bar()
bar.callDerivedFunc()

一直以为这两种调用方法是一样的, 求教两者区别的详细解释
8056 次点击
所在节点    Python
7 条回复
messense
2013-01-02 18:52:18 +08:00
直接调用 self._func() 的话,python 应该是优先查找 self 里面有没有 _func 这个方法,它发现没有,所以就出错了。
messense
2013-01-02 18:56:40 +08:00
试试

def callDerivedFunc(self):
getattr(self, '_func')(self)
xsaps
2013-01-02 19:17:39 +08:00
@messense 试了这样可以, 应该是和上面正确的调用形式一样
yuelang85
2013-01-02 19:23:54 +08:00
self._func = Bar.hello
这一行将Bar类的hello函数赋值给self._func变量,而不是实例的hello函数,而hello函数是一个实例方法。

method(self)调用没有错误,是因为给Bar.hello传递了一个实例,相当于:Bar.hello(bar)

self._func()调用出错,是因为没有Bar.hello传递实例,相当于:Bar.hello()。所以会报错:"TypeError: unbound method hello() must be called with Bar instance as first argument (got nothing instead)"


修改方法:

def __init__(self):
super(Foo, self).__init__()
self._func = Bar.hello
改成:
def __init__(self):
super(Foo, self).__init__()
self._func = self.hello
同时:
method(self)
改成:
method()


或者:

self._func() # wrong
改成:
self._func(self) # wrong
yuelang85
2013-01-02 19:31:20 +08:00
用gist做了个好看版:D

<script src="https://gist.github.com/4433964.js"></script>
yuelang85
2013-01-02 19:38:22 +08:00
上面那个失败了,重新学习了一下:

http://gist.github.com/4433964
xsaps
2013-01-02 20:19:01 +08:00
@yuelang85 thx, 是我混淆了直接调用成员函数和把成员变量当成函数调用的方法, 一开始还以为是self用法的问题...

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/56420

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX