A Better Way to Call the Super Constructor in Python 2.7

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

To call the constructor of a parent class in Python 2.7, the standard code that I ve seen is:

super(Child, self).__init__(self, val),

where Child is the child class. (In Python 3.x, this is simplified, but I have to use 2.7 for now.) My question is, wouldn t it be better for the "canonical" use of super in python 2.7 to be:

super(self.__class__, self).__init__(self, val)?

I ve tested this, and it seems to work. Is there any reason not to use this approach?

Answers

In Python 3.x, this is simplified, but I have to use 2.7 for now.

http://python-future.org/overview.html#code-examples http://python-future.org/overview.html#code-examples

from builtins import super  # from Python-Future

class Parent(object):
    def __init__(self):
        print( Hello )

class Child(Parent):
    def __init__(self):
        super().__init__()


Child()

Output:

Hello

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/41705001/a-better-way-to-call-the-super-constructor-in-python-2-7

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils