前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于python类中继承问题和描述器

关于python类中继承问题和描述器

作者头像
python与大数据分析
发布2022-03-11 13:56:40
3380
发布2022-03-11 13:56:40
举报
文章被收录于专栏:python与大数据分析

有句话叫躲得过初一躲不过十五,学习python过程中也有很多坎儿是必须要面对的,不管愿不愿意,都要去尝试,或许几遍都搞不懂,但还是要试,要不然一直没法提升自己。

类里面有好多内容,例如继承、多重继承、封装、描述器、属性、上下文管理、委托,装饰器等等,还有很多设计模式,这方面是我欠缺比较多的,其他知识还在思考阶段,先玩玩这个吧。

代码语言:javascript
复制
#关于继承中的两种调用方法
# 直接掉父类方法,有点像调用其他内部方法一样,每种方法都会被调用
# 另外一种是使用super(),
# 为了实现继承,Python 会在 MRO 列表上从左到右开始查找基类,直到找到第一个匹配这个属性的类为止。
# 使用 super() 函数时,Python 会在 MRO 列表上继续搜索下一个类。只要每个重定义的方法统一使用 super() 并只调用它一次
class Base:
    def __init__(self,type):
        print('Base.__init__')
class A(Base):
    def __init__(self,type):
        self.type=type
        if self.type=='Base':
            Base.__init__(self,self.type)
        elif self.type=='super':
            super().__init__(self.type)
        print('A.__init__')
class B(Base):
    def __init__(self,type):
        self.type=type
        if self.type=='Base':
            Base.__init__(self,self.type)
        elif self.type == 'super':
            super().__init__(self.type)
        print('B.__init__')
class AB(A,B):
    def __init__(self,type):
        self.type=type
        if self.type=='Base':
            A.__init__(self,self.type)
            B.__init__(self,self.type)
        elif self.type == 'super':
            super().__init__(self.type)
        print('C.__init__')

# Descriptor attribute for an integer type-checked attribute
class Integer:
    # 初始化对象的实例名
    # 描述器可实现大部分 Python 类特性中的底层魔法,包括 @classmethod 、@staticmethod 、@property ,甚至是 __slots__ 特性。
    # 描述器只能在类级别被定义
    # 操作实例底层的字典 (__dict__ 属性)
    # 描述器的 self.name 属性存储了在实例字典中被实际使用到的 key
    # 描述器的 字典值 属性存储了在实例字典中被实际使用到的 value
    def __init__(self, name):
        self.name = name
    #把实例名放入字典
    def __get__(self, instance, cls):
        #print('instance name=', instance)
        if instance is None:
            return self
        else:
            return instance.__dict__[self.name]
    #把实例名和值写入字典
    def __set__(self, instance, value):
        if not isinstance(value, int):
            raise TypeError('Expected an int')
        instance.__dict__[self.name] = value
    #根据键值到字典中删除
    def __delete__(self, instance):
        del instance.__dict__[self.name]

class Point:
    x = Integer('x')
    y = Integer('y')
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return '({},{})'.format(self.x,self.y)

print("------------base=Base('Base')--------------")
base=Base('Base')
# Base.__init__
print("------------abase=A('Base')--------------")
abase=A('Base')
# Base.__init__
# A.__init__
print("------------bbase=A('Base')--------------")
bbase=A('Base')
# Base.__init__
# A.__init__
print("------------asuper=A('super')--------------")
asuper=A('super')
# Base.__init__
# A.__init__
print("------------bsuper=B('super')--------------")
bsuper=B('super')
# Base.__init__
# B.__init__
print("------------abbase = AB('Base')--------------")
abbase = AB('Base')
# Base.__init__
# A.__init__
# Base.__init__
# B.__init__
# C.__init__
print("------------absuper = AB('super')--------------")
absuper = AB('super')
# Base.__init__
# B.__init__
# A.__init__
# C.__init__
print(AB.__mro__)
#(<class '__main__.AB'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Base'>, <class 'object'>)

#初始化Point实例
p=Point(2,3)
print(p)    #(2,3)
p.x=10
print(p)    #(10,3)
#p.y=3.5
#print(p)
#TypeError: Expected an int
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python与大数据分析 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档