我可以super()
在 Python 2.5.6 中使用干净的 Python 3 语法吗?
也许有某种__future__
进口?
4 回答
我意识到这个问题已经过时了,当时选择的答案可能是正确的,但它不再完整。您仍然不能super()
在 2.5.6 中使用,但为 2.6+python-future
提供了一个向后移植的实现:
安装python-future
:
% pip install future
下面显示了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'
它可以按如下方式使用:
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()
哪个输出
foo
bar
如果您使用pylint
,您可以使用以下注释禁用遗留警告:
# pylint: disable=missing-super-argument
您不能使用不super()
包含类型/类的裸调用。您也无法实施可行的替代方案。super()
Python 3.x 包含启用裸调用的特殊支持(它__class__
在类中定义的所有函数中放置一个单元变量 - 请参阅 PEP 3135
更新
从 Python 2.6+ 开始,可以通过Python 包使用裸super()
调用。有关解释,请参阅posita 的答案。future
你不能。但是您可以super()
在 Python 3 中使用 Python 2。
注意这是一个糟糕的“解决方案”,我发布它只是为了确保你不要在家里这样做!
我再说一遍:不要这样做
有人可能会考虑使用这个 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__()
,a 的构造B
将导致A
构造函数无限期地调用自身,因为self.__class__
will保持B
。这是由于在接受的答案__class__
中没有提到。您可以使用隐藏状态机或复杂的元类来解决这个问题,例如检查实际类在 中的位置,但这真的值得吗?可能不是...self.__class__.mro()