python super()的用法

我用的是python2.7,看了看菜鸟教程的例子:

class A(object):
    def add(self,x):
        y = x+1
        print(y)
        
class B(A):
    def add(self,x):
        super(B,self).add(x) 
        #括号中 self 是B的对象,通过super方法把B的对象 self 转换为父类A的对象,并且紧接着 
        #super(B,self).add(x)调用的是父类的方法add(),而不是子类B的add方法。再看下面的例子:

b = B()
b.add(2)
# 3

对于python2有这么几个需要注意的地方:

1、python2中被继承的类A必须继承一个object类,即要继承一个新式类super才能正常工作,而python3却不用,因为python3中所有的类都是新式类。

2、super(B,self)中,B的位置必须是一个type(类),self是这个type的一个实例化对象或者子类。比如b是B的一个实例化对象,self就表示b,一个B的实例化对象。

3、正确使用super方法可以直接使B的实例化对象b也像A的对象一般直接调用A中的方法并进行运算。

 另一个例子来说明super的用法(来自菜鸟教程):

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class FooParent(object):
    def __init__(self):
        self.parent = 'I\'m the parent.'
        print ('Parent')
    
    def bar(self,message):
        print ("%s from Parent" % message)
 
class FooChild(FooParent):
    def __init__(self):
        # super(FooChild,self) 首先找到 FooChild 的父类(就是类 FooParent),
        # 然后把类B的对象 FooChild 转换为类 FooParent 的对象
        super(FooChild,self).__init__()    
        print ('Child')
        
    def bar(self,message):
        super(FooChild, self).bar(message)
        print ('Child bar fuction')
        print (self.parent)
 
if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')

# 执行结果
Parent
Child
HelloWorld from Parent
Child bar fuction
I'm the parent.

执行过程是:从main函数开始,

1、先是fooChild是子类FooChild()的一个实例化对象,fooChild是一个对象;在初始化 fooChild 对象时自动调用了FooChild的__init__()方法,这样就触发了 super(FooChild,self).__init__(),这样就把FooChild的对象实例 fooChild 转换为父类 FooParent 的对象 并调用父类的__init__()方法,这样就给父类的属性self.parent 赋值了字符串值“I'm the parent” ,并执行__init__()方法中的 print('Parent') 输出 Parent;然后继续执行FooChild的__init__()方法中的 print('Child') ,输出 Child。

2、然后fooChild作为FooChild类的实例化对象调用类FooChild的 bar 方法并传入参数 message= ‘HelloWorld’ ;在调用FooChild的 bar 方法时首先遇到的是 super(FooChild, self).bar(message),这样又把 fooChild转化为父类FooParent的对象并调用父类的bar()方法,输出的是 HelloWorld from Parent 。

3、然后再回到子类FooChild的bar()方法,执行print('Child bar function') 输出 Child bar function ;

4、然后再次执行子类的bar()方法的print(self.parent),注意FooChild是继承了FooParent的子类,也就继承了父类的属性和方法,这样FooChild的实例化对象 fooChild 也继承了父类的属性 parent,这样执行print(self.parent)时相当于用子类的实例化对象调用父类的属性,那此时这个属性的值到底只属于这个子类专有的呢还是都是父类属性的值而子类的对象只是相当于能调用这个属性呢?如果是后者那么如果我们另写一个子类继承父类,则这个子类在不经过前面的子类操作的情况下调用父类属性self.parent时应该会得到和前一个子类相同的值.这个在菜鸟教程中没有说,我便继续加一个子类,弄明白这个问题,请继续看:

# 接着上面的代码写
class FooChild2(FooParent):
    def __init__(self):
#         super(FooChild2,self).__init__()
        print "It's foochild2"
        
    def bar(self,message):
        super(FooChild2, self).bar(message)
        print(self.parent)

if __name__ == '__main__':
    fooChild = FooChild()
    fooChild.bar('HelloWorld')
    fooChild2 = FooChild2()
    fooChild2.bar('Hello')

我定义了第二个子类FooChild2,也继承了 FooParent,不同的是我把FooChild2中的__init__()方法中 super(FooChild2,self).__init__()给注释掉了,这样导致子类FooChild2 在实例化对象时让它的实例化对象没有初始化父类的属性,导致子类2的实例没有继承到父类的属性。由此可以看出,继承是一种“拥有”的关系,即子类1对父类的继承的属性仅属于子类1,而不属于子类修改父类属性而后只是调用父类属性值这样,否则的话后面的子类在继承父类的值不就相当于和前面的子类继承了相同的父类属性值,不利于类的使用的灵活性,故子类继承属于前面说过的第一种情况,即子类继承了父类的属性,相当于把这个父类的属性隐含的添加到自己子类的定义中去了,属于这个子类所私有,其他子类再继承相同的父类时需自行定义父类的属性值。

把FooChild2 __init__()方法中的注释还原后便正常了:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值