V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-06-07 15:21:44 +08:00 · 2405 次点击
    这是一个创建于 2880 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2016-06-07 21:48:58 +08:00
    lzjun
        1
    lzjun  
       2016-06-07 15:27:43 +08:00
    正确的继承姿势:

    class Stragety(Temp):
    def __init__(self, afile):
    super(Stragety, self).__init__(afile)
    kingmo888
        2
    kingmo888  
    OP
       2016-06-07 15:27:46 +08:00
    问题已经解决。
    python3 中,重载时 super 不需要参数了。
    ```
    super().__init__(afile)
    ```
    我自己搞混了 py2 和 py3 ,最近在两个版本之间来回切换 - -!
    kingmo888
        3
    kingmo888  
    OP
       2016-06-07 15:29:04 +08:00
    1 楼运行下来也是 ok 的。 T.T
    lixiaohan
        4
    lixiaohan  
       2016-06-07 15:59:18 +08:00
    Temp 的 super 是 object, object 的 init 方法不需要参数, 提示里面已经说了, 你应该像 1 楼那样,
    mornlight
        5
    mornlight  
       2016-06-07 16:04:48 +08:00
    @kingmo888 这个地方的错误应该是用错了 super() 的参数
    wodemyworld
        6
    wodemyworld  
       2016-06-07 21:48:58 +08:00
    @kingmo888 最好永远像 1L 这样写,免得 py2 、 3 互相不兼容
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2795 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:04 · PVG 12:04 · LAX 21:04 · JFK 00:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.