我可以在 Python 2.5.6 中使用 Python 3 super() 吗?

发布于 2024-12-08 21:01:01 字数 148 浏览 0 评论 0原文

我可以在 Python 2.5 中使用干净的 Python 3 super() 语法吗? 6?
也许使用某种 __future__ 导入?

Can I use clean Python 3 super() syntax in Python 2.5.6?
Maybe with some kind of __future__ import?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-12-15 21:01:01

我意识到这个问题已经很老了,所选的答案当时可能是正确的,但它不再完整。在 2.5.6 中您仍然无法使用 super(),但是 python-future 为 2.6+ 提供向后移植的实现

安装python-future 具有

% pip install future

:下面显示了 builtinssuper 的重新定义:

% python
...
>>> import sys
>>> sys.version_info[:3]
(2, 7, 9)
>>>
>>> super
<type 'super'>
>>>
>>> from builtins import *
>>> super
<function newsuper at 0x000000010b4832e0>
>>> super.__module__
'future.builtins.newsuper'

它可以按如下方式使用:

from builtins import super

class Foo(object):
    def f(self):
        print('foo')

class Bar(Foo):
    def f(self):
        super().f() # <- whoomp, there it is
        print('bar')

b = Bar()
b.f()

which 输出

foo
bar

如果您使用 pylint,您可以使用以下命令禁用旧版警告:评论:

# pylint: disable=missing-super-argument

I realize this question is old, and the selected answer may have been correct at the time, but it's no longer complete. You still can't use super() in 2.5.6, but python-future provides a back-ported implementation for 2.6+:

Install python-future with:

% pip install future

The following shows the redefinition of super under builtins:

% python
...
>>> import sys
>>> sys.version_info[:3]
(2, 7, 9)
>>>
>>> super
<type 'super'>
>>>
>>> from builtins import *
>>> super
<function newsuper at 0x000000010b4832e0>
>>> super.__module__
'future.builtins.newsuper'

It can be used as follows:

from builtins import super

class Foo(object):
    def f(self):
        print('foo')

class Bar(Foo):
    def f(self):
        super().f() # <- whoomp, there it is
        print('bar')

b = Bar()
b.f()

which outputs

foo
bar

If you use pylint, you can disable legacy warnings with the comment:

# pylint: disable=missing-super-argument
最美不过初阳 2024-12-15 21:01:01

您不能使用不包含类型/类的裸 super() 调用。您也无法实现有效的替代方案。 Python 3.x 包含对启用裸 super() 调用的特殊支持(它将 __class__ 单元变量放置在类中定义的所有函数中 - 请参阅 PEP 3135


更新

从 Python 2.6+ 开始, 裸露super() 调用可以通过future Python 包使用。请参阅posita 的回答 进行解释。

You cannot use a bare super() call that contains no type/class. Nor can you implement a replacement for it that will work. Python 3.x contains special support to enable bare super() calls (it places a __class__ cell variable in all functions defined within a class - see PEP 3135


Update

As of Python 2.6+, bare super() calls can be used via the future Python package. See posita's answer for an explanation.

蓝海 2024-12-15 21:01:01

不,你不能。但是你可以使用Python 2的 super() Python 3.

No you cannot. But you can use Python 2's super() in Python 3.

夏花。依旧 2024-12-15 21:01:01

注意这是一个糟糕的“解决方案”,我发布它只是为了确保您不要在家里这样做!
我重复一遍:不要这样做

有人可能会考虑使用这个 mixin

class Super(object):
    def super(self):
        return super(self.__class__, self)

来获取 self.super()

class A(object, Super):
    def __init__(self):
        print "A"

class B(A):
    def __init__(self):
        print "B"
        self.super().__init__()

产生:

 >>> a = A()
 A
 >>> b = B()
 B
 A

但要注意:self.super() 不等于 super(B, self) - 如果 A 也称为 self.super()。 __init__(),构造一个B会导致 A 构造函数无限期地调用自身,因为 self.__class__ 将保留为 B。这是由于缺少接受的答案中提到的__class__。您可以使用隐藏状态机或复杂的元类来解决此问题,例如检查 self.__class__.mro() 中实际类的位置,但这真的值得吗?可能不是...

Note This is a terrible "solution", I post it only to make sure you don't do this at home!
I repeat: do not do this

One may think about using this mixin

class Super(object):
    def super(self):
        return super(self.__class__, self)

to obtain a self.super():

class A(object, Super):
    def __init__(self):
        print "A"

class B(A):
    def __init__(self):
        print "B"
        self.super().__init__()

yielding:

 >>> a = A()
 A
 >>> b = B()
 B
 A

But beware: This self.super() is not equivalent to super(B, self) - if A also called self.super().__init__(), the construction of a B would cause the A constructor to call itself indefinitely, since self.__class__ will remain B. This is due to the lack of the __class__ mentioned in the accepted answer. You can possibly work around this issue with a hidden state machine or a sophisticated metaclass that e.g. checks the actual class's position in self.__class__.mro(), but is it really worth it? Probably not...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文