跳转到主要内容

2.x中Python 3.x super()

项目描述

简介

newsuer 为您提供2.x中的Python 3.x super()

为什么

您可能讨厌编写这样的代码

super(Foo, self).__init__()

并且您可能已经编写了(错误的)这样的代码

super(self.__class__, self).__init__()

Python 3.x的super()可以救您于水火

super().__init__()

您想在2.x中使用它!

示例

只需将您的顶级类继承自 newsuer.Object

from newsuper import Object

class Foo(Object):
    def __init__(self):
        self.foo = 1

    @classmethod
    def do_something_with_class(cls):
        return [cls, 'Foo']

    @staticmethod
    def do_something_static():
        return ['Foo']

class Bar(Foo):
    def __init__(self):
        super().__init__()
        self.bar = 1

    @classmethod
    def do_something_static(cls):
        return super().do_something_static() + ['Bar']

class Baz(Bar):
    def __init__(self):
        super().__init__()
        self.baz = 1

    @classmethod
    def do_something_with_class(cls):
        # Compatible with old super(Baz, cls)
        return super(Baz, cls).do_something_with_class() + ['Baz']

    @staticmethod
    def do_something_static():
        # Yes, you can even super a staticmethod!
        return super().do_something_static() + ['Baz']

if __name__ == '__main__':
    assert Baz().foo == Baz().bar == Baz().baz == 1
    assert Baz.do_something_with_class() == [Baz, 'Foo', 'Baz']
    assert Baz.do_something_static() == ['Foo', 'Bar', 'Baz']

属性也受到支持。

安装

现在试试!

使用pip

pip install newsuper

或easy_install

easy_install newsuper

另外,还有一个Windows安装程序。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分布

newsuper-0.3c.zip (5.3 kB 查看哈希值)

上传时间:

构建分布

newsuper-0.3c.win32.exe (201.6 kB 查看哈希值)

上传时间:

支持者