python3 class super 用法

为了调用父类(超类)的一个方法,可以使用 super() 函数,比如

Python
# -*- coding: utf-8 -*- """ @Time: 2018/4/16 @Author: songhao @微信公众号: zeropython @File: d.py """ class One(object): def new_one(self): print("new_one func") class Two(One): def __init__(self): <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/super" title="View all posts in super" target="_blank">super</a></span>().new_one() def new_two(self): print("newtp") <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/super" title="View all posts in super" target="_blank">super</a></span>().new_one() if __name__ == '__main__': t = Two() t.new_two() """ 显示的结果是: new_one func newtp new_one func """
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
"""
@Time: 2018/4/16
@Author: songhao
@微信公众号: zeropython
@File: d.py
"""
 
class One ( object ) :
 
     def new_one ( self ) :
         print ( "new_one func" )
 
 
class Two ( One ) :
 
     def __init__ ( self ) :
         super ( ) . new_one ( )
 
     def new_two ( self ) :
         print ( "newtp" )
         super ( ) . new_one ( )
 
 
if __name__ == '__main__' :
     t = Two ( )
     t . new_two ( )
 
"""
显示的结果是:
new_one func
newtp
new_one func
"""

super() 函数的一个常见用法是在 __init__() 方法中确保父类被正确的初始化了:

Python
class A: def __init__(self): self.x = 0 class B(A): def __init__(self): super().__init__() self.y = 1
1
2
3
4
5
6
7
8
class A :
     def __init__ ( self ) :
         self . x = 0
 
class B ( A ) :
     def __init__ ( self ) :
         super ( ) . __init__ ( )
         self . y = 1

super() 的另外一个常见用法出现在覆盖Python特殊方法的代码中,比如:

Python
class Proxy: def __init__(self, obj): self._obj = obj # Delegate attribute lookup to internal obj def __getattr__(self, name): return getattr(self._obj, name) # Delegate attribute assignment def __setattr__(self, name, value): if name.startswith('_'): super().__setattr__(name, value) # Call original __setattr__ else: setattr(self._obj, name, value)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Proxy :
 
     def __init__ ( self , obj ) :
 
         self . _obj = obj
 
     # Delegate attribute lookup to internal obj
     def __getattr__ ( self , name ) :
 
         return getattr ( self . _obj , name )
 
     # Delegate attribute assignment
     def __setattr__ ( self , name , value ) :
 
         if name . startswith ( '_' ) :
             super ( ) . __setattr__ ( name , value )    # Call original __setattr__
         else :
             setattr ( self . _obj , name , value )

在上面代码中,__setattr__() 的实现包含一个名字检查。 如果某个属性名以下划线(_)开头,就通过 super() 调用原始的 __setattr__() , 否则的话就委派给内部的代理对象 self._obj 去处理。 这看上去有点意思,因为就算没有显式的指明某个类的父类, super() 仍然可以有效的工作。




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值