多重继承的时候, super 该怎么取参数才能继承到其他父类的方法? - 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

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

  •  
  •   napretep Apr 19, 2015 3812 views
    This topic created in 4041 days ago, the information mentioned may be changed or developed.
    --------------------------
    '''继承方法测试'''
    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 replies    2015-04-20 22:17:27 +08:00
    p1n3
        1
    p1n3  
       Apr 19, 2015
    这样定义就可以。。
    class B(S, A):
    wohenyingyu01
        2
    wohenyingyu01  
       Apr 19, 2015
    弱弱的问一下,你不会是上海仁泰酒店的那个人吧?
    napretep
        3
    napretep  
    OP
       Apr 19, 2015
    wohenyingyu01
        4
    wohenyingyu01  
       Apr 19, 2015
    @napretep 哦哦,不好意思,你头像和他一样
    laike9m
        5
    laike9m  
       Apr 19, 2015   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  
       Apr 19, 2015
    靠,v2居然没有代码高亮么。。。
    bombless
        7
    bombless  
       Apr 20, 2015
    谁告诉你v2是讨论代码的,233(逃
    laike9m
        8
    laike9m  
       Apr 20, 2015
    @bombless 我觉得代码高亮是个偏技术论坛的基本功能吧。。。就算不支持 markdown 手工<pre></pre>也无所谓啊。。
    bombless
        9
    bombless  
       Apr 20, 2015
    @laike9m 你需要QQ空间……据说对贴代码的支持很好(逃
    laike9m
        10
    laike9m  
       Apr 20, 2015
    @bombless 2333
    About     Help     Advertise     Blog     API     FAQ     Solana     3335 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 14:06 PVG 22:06 LAX 07:06 JFK 10:06
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86