V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 3, 2015 · 2997 views
    This topic created in 3901 days ago, the information mentioned may be changed or developed.

    有一个基类如下:

    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 replies    2015-09-03 23:49:34 +08:00
    c
        1
    c  
       Sep 3, 2015   ❤️ 1
    super (mail, self ).__init__('xxx')

    name = 'xxx'
    leisurelylicht
        2
    leisurelylicht  
    OP
       Sep 3, 2015
    @c 抱歉,我没怎么看明白,能麻烦您写的详细一点吗.
    seki
        3
    seki  
       Sep 3, 2015   ❤️ 1
    放在调用的 __init__ 里面
    这里就应该是 super ().__init__(name=xxx )
    leisurelylicht
        4
    leisurelylicht  
    OP
       Sep 3, 2015
    @seki 明白了,多谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3174 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 03:04 · PVG 11:04 · LAX 20:04 · JFK 23:04
    ♥ Do have faith in what you're doing.