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__()
...
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.
“[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.
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.
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.
Using super()
with arguments is backwards compatible, so you should just be able to use super(MyClass, self)
without needing to check the version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With