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

多重继承的时候, super 该怎么取参数才能继承到其他父类的方法?

  •  
  •   napretep · 2015-04-19 19:12:31 +08:00 · 3054 次点击
    这是一个创建于 3295 天前的主题,其中的信息可能已经有所发展或是发生改变。
    --------------------------
    '''继承方法测试'''
    class A(object):
    □□□□C = [3, 4]

    □□□□def __init__(self):
    □□□□□□□□self.__A = [1, 2]

    □□□□@property
    □□□□def get_A(self):
    □□□□□□□□return self.__A


    class S(object):
    □□□□W = [5, 6]

    □□□□def __init__(self):
    □□□□□□□□self.__S = [7, 8]

    空格@property
    □□□□def get_A(self):
    □□□□□□□□return self.__S


    class B(A, S):
    □□□□@property
    □□□□def get_B(self):
    □□□□□□□□return super(B, self).get_A[0]
    □□□□pass


    D = B()
    print(D.C)
    print(D.W)
    print(D.get_B)
    print(D.get_A)
    ----------------------------------------
    请问这条语句该怎么处理才能得到S类的方法?
    super(B, self).get_A[0]

    我想让print(D.get_B)执行时得到7的结果
    10 条回复    2015-04-20 22:17:27 +08:00
    p1n3
        1
    p1n3  
       2015-04-19 21:33:44 +08:00
    这样定义就可以。。
    class B(S, A):
    wohenyingyu01
        2
    wohenyingyu01  
       2015-04-19 21:37:09 +08:00
    弱弱的问一下,你不会是上海仁泰酒店的那个人吧?
    napretep
        3
    napretep  
    OP
       2015-04-19 22:50:20 +08:00
    wohenyingyu01
        4
    wohenyingyu01  
       2015-04-19 22:53:53 +08:00
    @napretep 哦哦,不好意思,你头像和他一样
    laike9m
        5
    laike9m  
       2015-04-19 23:19:57 +08:00   ❤️ 1
    ```
    >>> class A:
    pass

    >>> class S:
    pass

    >>> class B(A, S):
    def __init__(self):
    print(self.__class__.__mro__)


    >>> b = B()
    (<class '__main__.B'>, <class '__main__.A'>, <class '__main__.S'>, <class 'object'>)
    ```

    这个应该很明了吧
    laike9m
        6
    laike9m  
       2015-04-19 23:20:41 +08:00
    靠,v2居然没有代码高亮么。。。
    bombless
        7
    bombless  
       2015-04-20 09:50:07 +08:00
    谁告诉你v2是讨论代码的,233(逃
    laike9m
        8
    laike9m  
       2015-04-20 20:21:49 +08:00
    @bombless 我觉得代码高亮是个偏技术论坛的基本功能吧。。。就算不支持 markdown 手工<pre></pre>也无所谓啊。。
    bombless
        9
    bombless  
       2015-04-20 21:03:58 +08:00
    @laike9m 你需要QQ空间……据说对贴代码的支持很好(逃
    laike9m
        10
    laike9m  
       2015-04-20 22:17:27 +08:00
    @bombless 2333
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   977 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:53 · PVG 03:53 · LAX 12:53 · JFK 15:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.