推荐学习书目
› 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
kingmo888
V2EX  ›  Python

python 继承类出错?求解。

  •  
  •   kingmo888 · Jun 7, 2016 · 3429 views
    This topic created in 3805 days ago, the information mentioned may be changed or developed.
    class Temp(object):
        a = 0
        def __init__(self, afile):
            self.b = afile
        
        def deal(self):
            pass
    

    继承以上类:

    class Stragety(Temp):
        def __init__(self, afile):
            super(Temp, self).__init__(afile)
    

    提示出错:

    builtins.TypeError: object.init() takes no parameters

    明明有参数需求的嘛 - -!

    6 replies  •  2016-06-07 21:48:58 +08:00
    lzjun
    lzjun
    Jun 7, 2016
    1
    正确的继承姿势:

    class Stragety(Temp):
    def __init__(self, afile):
    super(Stragety, self).__init__(afile)
    kingmo888
    kingmo888
    OP
    Jun 7, 2016
    2
    问题已经解决。
    python3 中,重载时 super 不需要参数了。
    ```
    super().__init__(afile)
    ```
    我自己搞混了 py2 和 py3 ,最近在两个版本之间来回切换 - -!
    kingmo888
    kingmo888
    OP
    Jun 7, 2016
    3
    1 楼运行下来也是 ok 的。 T.T
    lixiaohan
    lixiaohan
    Jun 7, 2016
    4
    Temp 的 super 是 object, object 的 init 方法不需要参数, 提示里面已经说了, 你应该像 1 楼那样,
    mornlight
    mornlight
    Jun 7, 2016
    5
    @kingmo888 这个地方的错误应该是用错了 super() 的参数
    wodemyworld
    wodemyworld
    Jun 7, 2016
    6
    @kingmo888 最好永远像 1L 这样写,免得 py2 、 3 互相不兼容
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1533 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:49 · PVG 14:49 · LAX 22:49 · JFK 01:49
    ♥ Do have faith in what you're doing.