TypeError: super() 需要至少 1 个参数(0 给定)错误特定于任何 python 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38963018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
提问by BPL
I'm getting this error
我收到这个错误
TypeError: super() takes at least 1 argument (0 given)
类型错误:super() 至少需要 1 个参数(给定 0)
using this code on python2.7.11:
在 python2.7.11 上使用此代码:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super().__init__()
Bar()
The workaround to make it work would be:
使其工作的解决方法是:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
Bar()
It seems the syntax is specific to python 3. So, what's the best way to provide compatible code between 2.x and 3.x and avoiding this error happening?
似乎语法特定于 python 3。那么,在 2.x 和 3.x 之间提供兼容代码并避免发生此错误的最佳方法是什么?
采纳答案by Martijn Pieters
Yes, the 0-argument syntax is specific to Python 3, see What's New in Python 3.0and PEP 3135 -- New Super.
是的,0 参数语法特定于 Python 3,请参阅Python 3.0和PEP 3135 中的新增功能-新超级。
In Python 2 and code that must be cross-version compatible, just stick to passing in the class object and instance explicitly.
在 Python 2 和必须跨版本兼容的代码中,只需坚持显式传入类对象和实例。
Yes, there are "backports" available that make a no-argument version of super()
work in Python 2 (like the future
library) but these require a number of hacks that include a full scan of the class hierarchyto find a matching function object. This is both fragile and slow, and simply not worth the "convenience".
是的,有可用的“反向移植”可以super()
在 Python 2中制作无参数版本的工作(如future
库),但这些需要一些技巧,包括对类层次结构的完整扫描以找到匹配的函数对象。这既脆弱又缓慢,根本不值得“方便”。
回答by Viraj Wadate
This is because of version of python. Check your python version with [python --version] it might be 2.7
这是因为python的版本。用 [python --version] 检查你的 python 版本,它可能是 2.7
In 2.7 use this [ super(baseclass, self).__init__() ]
class Bird(object):
def __init__(self):
print("Bird")
def whatIsThis(self):
print("This is bird which can not swim")
class Animal(Bird):
def __init__(self):
super(Bird,self).__init__()
print("Animal")
def whatIsThis(self):
print("THis is animal which can swim")
a1 = Animal()
a1.whatIsThis()
> In 3.0 or more use this [ super().__init__()]
class Bird(object):
def __init__(self):
print("Bird")
def whatIsThis(self):
print("This is bird which can not swim")
class Animal(Bird):
def __init__(self):
super().__init__()
print("Animal")
def whatIsThis(self):
print("THis is animal which can swim")
a1 = Animal()
a1.whatIsThis()
回答by Laurent LAPORTE
回答by Manzur Alahi
Your default python --version
is probably python2, you need to switch to python3 to use this syntax, to do so paste the following command in your terminal.
您的默认python --version
值可能是 python2,您需要切换到 python3 以使用此语法,将以下命令粘贴到您的终端中。
sudo update-alternatives --config python
sudo update-alternatives --config python
If you get the error "no alternatives for python" then set up an alternative yourself with the following command:
如果您收到错误“python 无替代方案”,请使用以下命令自行设置替代方案:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
then check your python version with
然后检查你的python版本
python --version
python --version
if you get a version 3.+ then your problem is solved.
如果您获得 3.+ 版本,那么您的问题就解决了。