Dan*_*mov 15 python super python-2.5 python-3.x
我可以super()
在Python 2.5.6中使用干净的Python 3 语法吗?
也许有某种__future__
进口?
pos*_*ita 22
我意识到这个问题已经过时了,所选答案可能在当时是正确的,但它已不再完整.您仍然无法super()
在2.5.6中使用,但为2.6+ python-future
提供了后端实现:
安装时间python-future
:
% pip install future
Run Code Online (Sandbox Code Playgroud)
下面显示的重新定义super
下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'
Run Code Online (Sandbox Code Playgroud)
它可以使用如下:
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()
Run Code Online (Sandbox Code Playgroud)
哪个输出
foo
bar
Run Code Online (Sandbox Code Playgroud)
如果使用pylint
,您可以使用注释禁用旧版警告:
# pylint: disable=missing-super-argument
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4104 次 |
最近记录: |