Python中的Super方法实现问题及解决方案

1、问题背景

在Python中,super方法用于在子类中调用父类的方法。Guido van Rossum曾给出了一个纯Python实现的super方法,以便更好地理解其工作原理。然而,在这个实现中,存在一个问题:当传入的对象不是要调用的父类的实例时,该实现会出错。

2、解决方案

为了解决这个问题,需要对Guido的实现进行修改。具体来说,需要在__getattr__()方法中添加一个检查,以确保传入的对象是给定类型的实例。如果传入的对象不是给定类型的实例,那么就引发一个错误。

以下是修改后的__getattr__()方法:

python 复制代码
    def __getattr__(self, attr):
        if isinstance(self.__obj__, self.__type__):
            starttype = self.__obj__.__class__
        else:
            # 检查传入的对象是否是给定类型的实例
            if not isinstance(self.__obj__, self.__type__):
                raise TypeError("obj must be an instance or subtype of type")
            starttype = self.__obj__

        mro = iter(starttype.__mro__)
        for cls in mro:
            if cls is self.__type__:
                break
        # Note: mro is an iterator, so the second loop
        # picks up where the first one left off!
        for cls in mro:
            if attr in cls.__dict__:
                x = cls.__dict__[attr]
                if hasattr(x, "__get__"):
                    x = x.__get__(self.__obj__)
                return x
        raise AttributeError, attr

通过这个修改,就可以确保传入的对象是给定类型的实例,从而避免了错误的发生。

以下是修改后的代码示例:

python 复制代码
class Super(object):
    def __init__(self, type, obj=None):
        self.__type__ = type
        self.__obj__ = obj

    def __get__(self, obj, type=None):
        if self.__obj__ is None and obj is not None:
            return Super(self.__type__, obj)
        else:
            return self

    def __getattr__(self, attr):
        # 检查传入的对象是否是给定类型的实例
        if not isinstance(self.__obj__, self.__type__):
            raise TypeError("obj must be an instance or subtype of type")

        if isinstance(self.__obj__, self.__type__):
            starttype = self.__obj__.__class__
        else:
            starttype = self.__obj__

        mro = iter(starttype.__mro__)
        for cls in mro:
            if cls is self.__type__:
                break
        # Note: mro is an iterator, so the second loop
        # picks up where the first one left off!
        for cls in mro:
            if attr in cls.__dict__:
                x = cls.__dict__[attr]
                if hasattr(x, "__get__"):
                    x = x.__get__(self.__obj__)
                return x
        raise AttributeError, attr

class A(object):
    def m(self):
        '''  m in A'''
        return "A"

class B(A):
    def m(self):
        '''  m in B'''
        return "B" + Super(B, self).m()

class C(A):
    def m(self):
        ''' m in C '''
        return "C" + Super(C, self).m()

class D(C):
    def m(self):
        ''' m in D'''
        return "D" + Super(B, self).m()

print(D().m()) # "DCBA"

现在,该代码就可以正确地运行,并且不会引发错误。

相关推荐
m0_738120726 分钟前
渗透测试——Djinn1靶场详细渗透提权过程讲解(绕过黑名单限制,命令执行反弹shell,pyc反编译,代码白盒分析,python沙盒逃逸)
开发语言·python·php
Ares-Wang41 分钟前
AI》》欧氏距离、曼哈顿距离 切比雪夫距离 等
人工智能·python
陈eaten1 小时前
windows上协调多版本python以及虚拟环境
开发语言·windows·python·pycharm·pip·虚拟环境·py
一晌小贪欢1 小时前
告别 `datetime` 混乱:使用 Python 类型注解构建健壮的时间处理管道
开发语言·python·时间·时间类型·时间模块
嘛?2507011 小时前
Python高阶基础
python
li星野1 小时前
哈希表通关八题:从两数之和到LRU缓存,手撕高频面试题(Python + C++)
python·缓存·散列表
yaoxin5211231 小时前
401. Java 文件操作基础 - 使用 Buffered Stream I/O 写入文本文件
java·开发语言·python
S1998_1997111609•X1 小时前
哈希树函数洪水泛滥污染孪生镜像导致生物量子信息泄露以钩子而爬虫植入ssd探测
爬虫·网络协议·缓存·哈希算法·开闭原则
E_ICEBLUE1 小时前
如何提取 Word 文档中的表格并导出为 Excel(Python 教程)
python·word·excel
极光代码工作室1 小时前
基于NLP的智能问答系统设计
python·深度学习·自然语言处理·nlp