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 查看哈希值)
关闭
newsuer-0.3c.zip的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 6f57979d191a52b64c9c50ab608959c010e7b6539c4bcf31b9393ad3d1daf6da |
|
| MD5 | b2dbd52e4f06a0bef765a1a4a45ea288 |
|
| BLAKE2b-256 | dd610fd19538b105a40fe6c55739958f286fbfb778d54134cea44892d0fb00c4 |
关闭
newsuper-0.3c.win32.exe 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 47767103c262abcf7a5e64a5aed44eec3eebd0cf195aff2d474a9124c6bdc0d1 |
|
| MD5 | 13a3bce8f94bd2082db42daeaf6a0891 |
|
| BLAKE2b-256 | ce4329a5d6ef9716d7e27d750a940ff8a19c75c2fa1a54bb77458d058056bb13 |