Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call super() so it's compatible in 2 and 3?

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__()
        ...
like image 529
Nick T Avatar asked Feb 14 '14 00:02

Nick T


People also ask

What does super() return?

The super() function is used to give access to methods and properties of a parent or sibling class. The super() function returns an object that represents the parent class.

What is the argument is super()?

“[Super is used to] return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class.

What does super()__ init__ do?

When you initialize a child class in Python, you can call the super(). __init__() method. This initializes the parent class object into the child class. In addition to this, you can add child-specific information to the child object as well.

How does super work in Python?

The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.


1 Answers

Using super() with arguments is backwards compatible, so you should just be able to use super(MyClass, self) without needing to check the version.

like image 78
Andrew Clark Avatar answered Sep 23 '22 01:09

Andrew Clark