如何调用super()使其在2和3中兼容? [英] How can I call super() so it's compatible in 2 and 3?
本文介绍了如何调用super()使其在2和3中兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用six
编写2/3兼容代码,但是我看不到如何以交叉兼容的方式调用super()
.除此之外,还有其他更好的方法吗?
I'm trying to write 2/3 compatible code using six
, but I don't see how I can call super()
in a cross-compatible manner. Is there some better way besides, for example:
class MyClass(MyBase):
def __init__():
if six.PY3:
super().__init__()
else:
super(MyClass, self).__init__()
...
推荐答案
使用 super()
带参数的是向后兼容的,因此您只需使用super(MyClass, self)
即可,而无需检查版本.
Using super()
with arguments is backwards compatible, so you should just be able to use super(MyClass, self)
without needing to check the version.
这篇关于如何调用super()使其在2和3中兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文