[Kde-bindings] Python Qt4 Newbie

Ruben Fonseca rf at 7syntax.com
Sun Aug 24 18:14:24 UTC 2008


On Thu, 2008-08-21 at 10:50 +0100, Ruben Fonseca wrote:
> > >>> x = lambda : a*4
> > >>> x()
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> >   File "<stdin>", line 1, in <lambda>
> > NameError: global name 'a' is not defined
> > >>> a = 3
> > >>> x()
> > 12
> 
> Isn't it possible to define real anonymous functions in Python, without
> this lexical/binding thing?

I guess I just found the answer to my problem :)

------

from functools import partial

for button in [self.button_1, self.button_2, ..., self.button_0]:
    QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'),
partial(self.button_clicked, button))

def button_clicked(self, button):
    print "yeah!"

------

works like a charm, and seems elegant enough to me :)

Cheers,
Ruben
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20080824/90252e3b/attachment-0001.sig>


More information about the Kde-bindings mailing list