TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
我遇到了这个错误
TypeError: super() takes at least 1 argument (0 given)
在 python2.7.11 上使用此代码:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super().__init__()
Bar()
使其工作的解决方法是:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
Bar()
似乎语法特定于 python 3. 那么,在 2.x 和 3.x 之间提供兼容代码并避免发生此错误的最佳方法是什么?
您可以使用 future 库以获得 Python2/Python3 兼容性。
super 函数已向后移植。
是的,0 参数语法特定于 Python 3,请参阅 What's New in Python 3.0 and PEP 3135 -- New Super。
在Python2和必须跨版本兼容的代码中,坚持显式传入class对象和实例即可。
是的,有 "backports" 可以使 super()
的无参数版本在 Python 2 中工作(如 future
库),但这些需要包含 full scan of the class hierarchy 以查找匹配函数对象的 hack 数量。这既脆弱又缓慢,根本不值得 "convenience"。
这是因为 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()
您的默认 python --version
可能是 python2,您需要切换到 python3 才能使用此语法,为此请在您的终端中粘贴以下命令。
sudo update-alternatives --config python
如果您收到错误 "no alternatives for python",请使用以下命令自行设置替代方案:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
然后用
检查你的 python 版本
python --version
如果您获得版本 3.+,那么您的问题就解决了。
对于python 3.6
super(Bird,self).init() 不工作
super(Animal,self).init() 为我工作。
我遇到了这个错误
TypeError: super() takes at least 1 argument (0 given)
在 python2.7.11 上使用此代码:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super().__init__()
Bar()
使其工作的解决方法是:
class Foo(object):
def __init__(self):
pass
class Bar(Foo):
def __init__(self):
super(Bar, self).__init__()
Bar()
似乎语法特定于 python 3. 那么,在 2.x 和 3.x 之间提供兼容代码并避免发生此错误的最佳方法是什么?
您可以使用 future 库以获得 Python2/Python3 兼容性。
super 函数已向后移植。
是的,0 参数语法特定于 Python 3,请参阅 What's New in Python 3.0 and PEP 3135 -- New Super。
在Python2和必须跨版本兼容的代码中,坚持显式传入class对象和实例即可。
是的,有 "backports" 可以使 super()
的无参数版本在 Python 2 中工作(如 future
库),但这些需要包含 full scan of the class hierarchy 以查找匹配函数对象的 hack 数量。这既脆弱又缓慢,根本不值得 "convenience"。
这是因为 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()
您的默认 python --version
可能是 python2,您需要切换到 python3 才能使用此语法,为此请在您的终端中粘贴以下命令。
sudo update-alternatives --config python
如果您收到错误 "no alternatives for python",请使用以下命令自行设置替代方案:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
然后用
检查你的 python 版本python --version
如果您获得版本 3.+,那么您的问题就解决了。
对于python 3.6 super(Bird,self).init() 不工作 super(Animal,self).init() 为我工作。