Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python inheritance and __init__ functions

Tags:

python

pyqt

I came across the folloqing type of code when looking for some pyQt examples :

class DisplayPage(QWizardPage):
    def __init__(self, *args):
        apply(QWizardPage.__init__, (self, ) + args)

What does *args mean ?
What is the purpose of using apply for this type of code ?

like image 302
shodanex Avatar asked Nov 18 '25 21:11

shodanex


2 Answers

*args means that __init__ takes any number of positional arguments, all of which will be stored in the list args. For more on that, see What does *args and **kwargs mean?

This piece of code uses the deprecated apply function. Nowadays you would write this in one of three ways:

 QWizardPage.__init__(self, *args)
 super(DisplayPage, self).__init__(*args)
 super().__init__(*args)

The first line is a literal translation of what apply does (don't use it in this case, unless QWizardPage is not a new-style class). The second uses super as defined in PEP 367. The third uses super as defined in PEP 3135 (works only in Python 3.x).

like image 90
Stephan202 Avatar answered Nov 20 '25 12:11

Stephan202


DisplayPage inherits from QWizardPage. Its constructor accepts a variable amount of arguments (which is what *args means), and passes them all to the constructor of its parent, QWizardPage

It's better to say:

super(DisplayPage, self).__init__(*args)
like image 40
Eli Bendersky Avatar answered Nov 20 '25 13:11

Eli Bendersky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!