Created
January 2, 2013 11:31
-
-
Save laonger/4433964 to your computer and use it in GitHub Desktop.
python实例方法的调用问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# encoding: utf-8 | |
class Foo(object): | |
def __init__(self): | |
self._func = None | |
def callDerivedFunc(self): | |
self._func() # wrong #这里实际上等于:Bar.hello(), hello是一个实例方法,需要传递一个实例才能调用。所以报错,应该改成:self._func(self) | |
method = self._func | |
method(self) # right # 由于传递了一个实例self,相当于:Bar.hello(self),所以可以成功调用 | |
class Bar(Foo): | |
def __init__(self): | |
super(Foo, self).__init__() | |
self._func = Bar.hello | |
def hello(self): | |
print 'Hello' | |
bar = Bar() | |
bar.callDerivedFunc() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment