推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xsaps
V2EX  ›  Python

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

  •  
  •   xsaps · Jan 2, 2013 · 8980 views
    This topic created in 4888 days ago, the information mentioned may be changed or developed.
    在父函数中调用子函数的成员函数, 遇到个问题, 代码如下

    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()

    一直以为这两种调用方法是一样的, 求教两者区别的详细解释
    7 replies    1970-01-01 08:00:00 +08:00
    messense
        1
    messense  
       Jan 2, 2013
    直接调用 self._func() 的话,python 应该是优先查找 self 里面有没有 _func 这个方法,它发现没有,所以就出错了。
    messense
        2
    messense  
       Jan 2, 2013
    试试

    def callDerivedFunc(self):
    getattr(self, '_func')(self)
    xsaps
        3
    xsaps  
    OP
       Jan 2, 2013
    @messense 试了这样可以, 应该是和上面正确的调用形式一样
    yuelang85
        4
    yuelang85  
       Jan 2, 2013
    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
        5
    yuelang85  
       Jan 2, 2013
    用gist做了个好看版:D

    <script src="https://gist.github.com/4433964.js"></script>
    yuelang85
        6
    yuelang85  
       Jan 2, 2013
    上面那个失败了,重新学习了一下:

    http://gist.github.com/4433964
    xsaps
        7
    xsaps  
    OP
       Jan 2, 2013
    @yuelang85 thx, 是我混淆了直接调用成员函数和把成员变量当成函数调用的方法, 一开始还以为是self用法的问题...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2930 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 02:22 · PVG 10:22 · LAX 19:22 · JFK 22:22
    ♥ Do have faith in what you're doing.