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
leisurelylicht
V2EX  ›  Python

关于如何使用 super 函数继承带参数的父类的方法的请教.

  •  
  •   leisurelylicht · 2015-09-03 23:28:18 +08:00 · 2485 次点击
    这是一个创建于 3159 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个基类如下:

    class MailCreate (object ):
        def __init__(self,name ):
            super (MailCreate, self ).__init__()
            self.name = name
    

    它有一个参数 name.

    现在想建立一个新类 mail 并继承 MailCreate.

    class mail (MailCreate ):
        def __init__(self, arg ):
            super (mail, self ).__init__()
            self.arg = arg
    

    应该怎么用 super 给父类的 name 参数赋值?

    4 条回复    2015-09-03 23:49:34 +08:00
    c
        1
    c  
       2015-09-03 23:33:27 +08:00   ❤️ 1
    super (mail, self ).__init__('xxx')

    name = 'xxx'
    leisurelylicht
        2
    leisurelylicht  
    OP
       2015-09-03 23:40:27 +08:00
    @c 抱歉,我没怎么看明白,能麻烦您写的详细一点吗.
    seki
        3
    seki  
       2015-09-03 23:46:05 +08:00   ❤️ 1
    放在调用的 __init__ 里面
    这里就应该是 super ().__init__(name=xxx )
    leisurelylicht
        4
    leisurelylicht  
    OP
       2015-09-03 23:49:34 +08:00
    @seki 明白了,多谢!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2888 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:50 · PVG 22:50 · LAX 07:50 · JFK 10:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.