-1

Using super in python 2.7 results in error code,

TypeError: super() takes at least 1 argument (0 given)

unitom
  • 129
  • 6
  • 3
    Note that Python 2 is deprecated and is no longer under support/development since 2020, so it shouldn't be used unless absolutely necessary. – Ted Klein Bergman Mar 15 '21 at 18:14
  • So when you copied and pasted the phrase `How to use Super() in Python 2.7 for multiple inheritance` into a search engine, and [looked over the various results](https://duckduckgo.com/?q=How+to+use+Super()+in+Python+2.7+for+multiple+inheritance), what exactly confused you? How about when you copied and pasted the [error message](https://duckduckgo.com/?q=TypeError%3A+super()+takes+at+least+1+argument+(0+given))? – Karl Knechtel Mar 15 '21 at 18:21
  • 1
    Does this answer your question? [How does Python's super() work with multiple inheritance?](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) – Michael Ruth Mar 15 '21 at 18:22

1 Answers1

1

Following code can help in understanding how to use the super function for multiple inheritance in python 2.7,

class A(object):
    def __init__(self):
        print("Class A")
        super(A, self).__init__()


class B(object):
    def __init__(self):
        print("Class B")
        super(B, self).__init__()


class C(A, B):

    def __init__(self):
        print("Class C")
        super(C, self).__init__()


Cobj = C()

In Python 3, super function is a bit simplified and can be used as follows,

class A:
    def __init__(self):
        print("Class A")
        super().__init__()


class B:
    def __init__(self):
        print("Class B")
        super().__init__()


class C(A, B):

    def __init__(self):
        print("Class C")
        super().__init__()


Cobj = C()

If you run the above code in python 2.7, you will get this error message,

TypeError: super() takes at least 1 argument (0 given)

Most books assume python- 3.x is being used in all productions, which is not the case. So code style and patterns from 2.7 are still very relevant for developers.

Dharman
  • 30,962
  • 25
  • 85
  • 135
unitom
  • 129
  • 6