Skip to content

Instantly share code, notes, and snippets.

@laonger
Created January 2, 2013 11:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laonger/4433964 to your computer and use it in GitHub Desktop.
Save laonger/4433964 to your computer and use it in GitHub Desktop.
python实例方法的调用问题
#!/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