搜索
首页后端开发Python教程Python类中如何定义多个构造器方法重载和泛方法

    什么是“泛方法”?

    由多个方法组成的方法,这些方法为不同的类型实现相同的操作。

    举个栗子 

    现在有个需求,需要你通过以下几种方式创建一个自定义的日期类(CustomDate):

    • 时间戳

    • 年、月、日(包含三个整数的元组)

    • ISO 格式的字符串

    • Datetime

    一般实现

    from datetime import date, datetime
    class CustomDate:
        def __init__(self, arg):
            if isinstance(arg, (int, float)):
                self.__date = date.fromtimestamp(arg)
            elif isinstance(arg, tuple) and len(arg) == 3 and all(map(lambda x: isinstance(x, int), arg):
                self.__date = date(*arg)
            elif isinstance(arg, str):
                self.__date = date.fromisoformat(arg)
            elif isinstance(arg, datetime):
                self.__date = datetime.date()
            else:
                raise TypeError("could not create instance from " + type(arg).__name__)
        @property
        def date():
            return self.__date

    注:这里暂不讨论传入的日期/时间戳合不合法,仅仅只对类型做大致判断。

    有没有更好的方式?

    我们可以将不同的构建方式拆分为多个方法,并利用 functools 中的 singledispatchmethod 装饰器来根据传入的参数类型决定调用哪个方法。

    from datetime import date, datetime
    from functools import singledispatchmethod
    class CustomDate:
        @singledispatchmethod
        def __init__(self, arg):
            raise TypeError("could not create instance from " + type(arg).__name__)
        @__init__.register(int)
        @__init__.register(float)
        def __from_timestamp(self, arg):
            self.__date = date.fromtimestamp(arg)
        @__init__.register(tuple)
        def __from_tuple(self, arg):
            if len(arg) == 3 and all(map(lambda x: isinstance(x, int), arg)):
                self.__date = date(*arg)
            else:
                raise ValueError("could not create instance from a malformed tuple")
        @__init__.register(str)
        def __from_isoformat(self, arg):
            self.__date = date.fromisoformat(arg)
        @__init__.register(datetime)
        def __from_datetime(self, arg):
            self.__date = arg.date()
        @property
        def date(self):
            return self.__date

    这样一来,我们便能将每种参数类型的初始化独立成一个个的方法了。

    缺点

    #1 单分派

    在调用期间应该使用哪个方法实现由分派算法决定。如果该算法只基于单个参数的类型来决定使用哪个方法实现,则称其为单分派。

    singledispatchmethod 就是就是单分派的。也就是说,只有第一个参数会作为考量。这在实际业务中是远远不足的。

    #2 不支持 typing

    然而,如上,对元组中元素类型判断还是需要我们用 if/else 实现。也就是说,我们不能使用 typing.Tuple[int, int, int]

    作为一种折中的方案,或许我们可以定义一个 ThreeIntTuple 类来对其进行限定,将这些判断从 CustomDate 类中隔离开来。

    这里仅提供一个思路让大家参考,我就不实现了(因为我们有更好的方式 xD)。

    替代方案:multimethod 库

    这个库不是标准库之一,需要通过 pip 安装:

    pip install multimethod

    优势

    multimethod 采用的是多分派算法,能更好地满足更复杂的场景。此外,该库对 typing 中的类型也有不错的支持。

    更更好的实践方式

    回到上面的问题,我们可以这么改进:

    • 使用 multimethod 方法来替代 singledispatchmethod

    • 使用 Tuple[int, int, int] 来替代 tuple,不再需要手动校验元组的长度和元素类型了;

    from datetime import date, datetime
    from typing import Tuple, Union
    from multimethod import multimethod
    class CustomDate:
        @multimethod
        def __init__(self, arg):
            raise TypeError("could not create instance from " + type(arg).__name__)
        @__init__.register
        def __from_timestamp(self, arg: Union[int, float]):
            self.__date = date.fromtimestamp(arg)
        @__init__.register
        def __from_tuple(self, arg: Tuple[int, int, int]):
            self.__date = date(*arg)
        @__init__.register
        def __from_isoformat(self, arg: str):
            self.__date = date.fromisoformat(arg)
        @__init__.register
        def __from_datetime(self, arg: datetime):
            self.__date = arg.date()
        @property
        def date(self):
            return self.__date

    究极好的实践方式(真正的方法重载)

    在此之前,先问大家一个简单的问题(这跟我们之后的内容有很大的联系):

    class A:
        def a(self):
            print(1)
        def a(self):
            print(2)
    A().a()

    以上这段代码会输出什么?还是会抛出错误?

    输出 2

    在 Python 中,如果定义了重名的方法,最后一个方法是会覆盖掉之前的方法的。

    但你或许不知,我们可以通过元类(metaclass)来改变这一行为:

    class MetaA(type):
        class __prepare__(dict):
            def __init__(*args):
                pass
            def __setitem__(self, key, value):
                if self.get('a'):  # Line 7
                    super().__setitem__('b', value)  # Line 8
                else:	
                    super().__setitem__(key, value)
    class A(metaclass=MetaA):
        def a(self):
            print(1)
        def a(self):
            print(2)
    A().a()  # => 1
    A().b()  # => 2  # Line 22

    在第 7 和第 8 行,我们将重名的 a 方法改名为 b,并在第 22 行成功地调用它了。

    multimethod 的维护者们很好地运用了这一点,对重名的方法进行了处理,以达到一种“特殊的效果”。

    回到正题,我们可以做出如下改进:

    • multimethod.multidata 设置为 CustomDate 类的元类;

    • 将所有方法命名为 __init__

    from datetime import date, datetime
    from typing import Tuple, Union
    from multimethod import multimeta
    class CustomDate(metaclass=multimeta):
        def __init__(self, arg: Union[int, float]):
            self.__date = date.fromtimestamp(arg)
        def __init__(self, arg: Tuple[int, int, int]):
            self.__date = date(*arg)
        def __init__(self, arg: str):
            self.__date = date.fromisoformat(arg)
        def __init__(self, arg: datetime):
            self.__date = arg.date()
        def __init__(self, arg):
            raise TypeError("could not create instance from " + type(arg).__name__)
        @property
        def date(self):
            return self.__date

    从效果上来看,这完全和静态语言的方法重载一模一样!

    以上是Python类中如何定义多个构造器方法重载和泛方法的详细内容。更多信息请关注PHP中文网其他相关文章!

    声明
    本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
    Python中的合并列表:选择正确的方法Python中的合并列表:选择正确的方法May 14, 2025 am 12:11 AM

    Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

    如何在Python 3中加入两个列表?如何在Python 3中加入两个列表?May 14, 2025 am 12:09 AM

    在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

    Python串联列表字符串Python串联列表字符串May 14, 2025 am 12:08 AM

    使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

    Python执行,那是什么?Python执行,那是什么?May 14, 2025 am 12:06 AM

    pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

    Python:关键功能是什么Python:关键功能是什么May 14, 2025 am 12:02 AM

    Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

    Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

    Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

    python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

    useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

    Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

    pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

    See all articles

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热门文章

    热工具

    SublimeText3 Linux新版

    SublimeText3 Linux新版

    SublimeText3 Linux最新版

    SecLists

    SecLists

    SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    功能强大的PHP集成开发环境

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器