[PyKDE] beginner question
boud at rempt.xs4all.nl
Thu Oct 28 19:24:25 BST 1999
On Thu, 28 Oct 1999, Neal Becker wrote:
> Many examples contain the following construct:
> def __init__(self):
> def __init__(self,*args):
> apply(QWidget.__init__,(self,) + args)
> what does this do exactly?
I must admit that _apply_ stumped me for quite a while, too. I started
using PyKDE/Qt before I had anything near proficiency in Python, which
is why in a lot of cases this idiom is present in the tutorial where it
If you subclass a class and you write a new __init__ function for your
subclass, then the __init__ function of the superclass won't be called,
unless you explicitly call the superclass's __init__ function. Not
initializing a Qt class generally leads to a segfault ;-).
Now Qt classes can take a lot of parameters upon initialisation, and
you might not want to specify all those parameters in your __init__
function (they might change in the next version of Qt, for instance),
even though you might want to be able to pass them to the original Qt
superclass when you create an object from your subclass. In that case
you can apply the arguments you passed to your __init__ function to the
__init__ function of the superclass by using the Python function _apply_.
_Apply_ is a basic Python function that executes a function with the
arguments given in a tuple, so _apply(f, (x,y))_ is exactly the same
as calling _f(x,y)_ - in the case if QWidget.__init__, the line you give
above is equivalent to QWidget.__init__(self, x, y, z), given that args
is a tuple of values for x, y and z: (x,y,z).
So, to start from the beginning:
The Qt class QWidgetThingy has an __init__ function like this:
def __init__(self, parent, name, f):
( Of course, this really is C++ - but I can't write that. Something
class Q_EXPORT QWidgetThingy : public QObject
QWidgetThingy( QWidgetThingy *parent=0, const char *name=0, WFlags f=0 );
but for us Pythoneers that's not terribly important.
Which you subclass:
def __init__(self, foo, bar, *args):
And you could create an instance of MyWidgetThingy as follows:
thing=MyWidgetThingy(f, b, parent, "xyz", 0)
thing=MyWidgetThingy(f, b, parent)
if you didn't care about the name or the window flags.
That's a bit more flexible than if you'd decided you'd only ever needed
a parent to your objects, and had declared:
def __init__(self, foo, bar, parent):
In which case you will not be able to create a MyWidgetThingy with a
window flags, unless you rewrite the class.
To conclude: in your first example _apply_ was uses without any need for
it, since no variable tuple of arguments was going to be passed anyway,
but in the second it could be useful.
I hope this was actually correct and intelligible ;-) - if not, let
please someone correct me. I'll put it somewhere near my tutorial,
close to the references section.
Boudewijn Rempt | http://www.xs4all.nl/~bsarempt
More information about the PyQt