python super研究

# encoding=utf-8

class A(object):
    def __init__(self):
        print "初始化a"

    def run(self):
        print "运行a"

class B(A):
    def __init__(self):
        print ‘开始初始化b‘
        super(B, self).__init__()
        print ‘结束初始化b‘

    def run(self):
        print "开始运行b"
        super(B, self).run()
        print ‘结束运行b‘

class C(B):
    def __init__(self):
        print ‘开始初始化c‘
        super(C, self).__init__()
        print ‘结束初始化c‘

    def run(self):
        print "开始运行c"
        super(C, self).run()
        print ‘结束运行c‘

if __name__ == ‘__main__‘:
    c = C()
    c.run()

# 结果是:
# 开始初始化c
# 开始初始化b
# 初始化a
# 结束初始化b
# 结束初始化c
# 开始运行c
# 开始运行b
# 运行a
# 结束运行b
# 结束运行c
# 从结果我们可以看出,super采取的是深度优先遍历继承
时间: 2024-08-08 22:08:30

python super研究的相关文章

理解python super()类

首先我们通过网友在stackoverflow关于python super类的一个疑问来作为本篇的开始,问题大致是这样的: >>> class A(object): ... def __init__(self): ... print "A init" ... super(A,self).__init__() ... >>> >>> a = A() A init >>> class B(object): ... def

Python super() 函数

Python super() 函数  Python 内置函数 描述 super() 函数是用于调用父类(超类)的一个方法. super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是如果使用多继承,会涉及到查找顺序(MRO).重复调用(钻石继承)等种种问题. MRO 就是类的方法解析顺序表, 其实也就是继承父类方法时的顺序表. 语法 以下是 super() 方法的语法: super(type[, object-or-type]) 参数 type -- 类. obj

python super()

一.问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如代码段1: 代码段1: class A: def __init__(self): print "enter A" print "leave A" class B(A): def __init__(self): print "enter B" A.__init__(self) print "leave B"

Python super初始化理解过程

Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__init__(xxx,xxx)”.那super().__init__()到底要怎么用呢? 为了方便理解,我写了如下例子验证用法: #-*- coding:utf-8 -*-class A(object): def __init__(self,xing,gender): #!#1 self.namea="a

python super()使用详解

1.super的作用调用父类方法2.单继承使用示例 #coding:utf-8 #单继承 class A(object): def __init__(self): self.n=2 def add(self,m): self.n+=m class B(A): def __init__(self): self.n=3 def add(self,m): super(B,self).add(m) self.n+=3 b=B() b.add(3) print b.n 运行结果: super(B,self

Python super继承详解

MRO(Method resolution order)是python用来解析方法调用顺序的,mro中记录了一个类的所有基类的类类型序列,super不是简单地调用基类的方法,而是按照MRO中的顺序来调用类的方法.使用super()时,应该在所有类中使用,否则就可能发生有的类构造函数没有调用的情况.#!/usr/bin/python# -*- coding: utf-8 -*-class A(object): def __init__(self): print 'A __init__' super

转载:python super()及相关引申

super()是Python类的方法(method),用来调用父类,superclass 即 超类(父类) 详细请看http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035005.html 用 [类名.mro()] 来查看 该类的mro的记录 关于Python 多重继承mro 详细请看:http://blog.sina.com.cn/s/blog_45ac0d0a01018488.html 关于深度优先和广度优先请看: http://ww

Python ---- super()使用

我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: 1 2 3 4 5 6 7 8 9 10 11 12 13 class A:     def func(self):         print('OldBoy') class B(A):     def func(self):         super().func()         print('LuffyCity') A().func() B().func() 输出的结果为: OldBoy OldBoy Luf

(转)Python: super 没那么简单

原文:https://mozillazg.com/2016/12/python-super-is-not-as-simple-as-you-thought.html python 约定? 单继承? 多继承? super 是个类? 多继承中 super 的工作方式? 参考资料? 说到 super, 大家可能觉得很简单呀,不就是用来调用父类方法的嘛.如果真的这么简单的话也就不会有这篇文章了,且听我细细道来.?? 约定 在开始之前我们来约定一下本文所使用的 Python 版本.默认用的是 Python