TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?

穿精又带淫゛_ 提交于 2019-11-28 18:08:39

Yes, the 0-argument syntax is specific to Python 3, see What's New in Python 3.0 and PEP 3135 -- New Super.

In Python 2 and code that must be cross-version compatible, just stick to passing in the class object and instance explicitly.

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 hierarchy to find a matching function object. This is both fragile and slow, and simply not worth the "convenience".

This is because of version of python. Check your python version with [python --version] it might be 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()

You can use the future library to have a Python2/Python3 compatibility.

The super function is back-ported.

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.

sudo update-alternatives --config python

If you get the error "no alternatives for python" then set up an alternative yourself with the following command:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10

then check your python version with

python --version

if you get a version 3.+ then your problem is solved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!