Events function at binding

I can’t
SyntaxError: lambda cannot contain assignment

···

----- Original Message ----
From: Cody Precord codyprecord@gmail.com
To: wxpython-users@lists.wxwidgets.org
Sent: Monday, August 18, 2008 5:27:39 PM
Subject: Re: [wxpython-users] Events function at binding

Hello,

Use lambda

self.btn.Bind(wx.EVT_BUTTON, lambda evt: self.attr.value = None)

Cody

On Aug 18, 2008, at 6:06 AM, Prashant Saxena wrote:

Hi,

self.btn.Bind(wx.EVT_BUTTON, self.attr.value = None)

This won’t work, is there any workaround for this instead of writing a function for a single line
instruction.

Thanks


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hello,

I can’t
SyntaxError: lambda cannot contain assignment

Sorry, forgot that assignment is not an operation but considered a statement in python. You would need to have a function for setting the variable. If your class already has a setter for that variable you can call that in the lambda.

i.e)

lambda evt: self.SetAttr(None)

Cody

···

On Aug 18, 2008, at 7:02 AM, Prashant Saxena wrote:

----- Original Message ----
From: Cody Precord codyprecord@gmail.com
To: wxpython-users@lists.wxwidgets.org
Sent: Monday, August 18, 2008 5:27:39 PM
Subject: Re: [wxpython-users] Events function at binding

Hello,

Use lambda

self.btn.Bind(wx.EVT_BUTTON, lambda evt: self.attr.value = None)

Cody

On Aug 18, 2008, at 6:06 AM, Prashant Saxena wrote:

Hi,

self.btn.Bind(wx.EVT_BUTTON, self.attr.value = None)

This won’t work, is there any workaround for this instead of writing a function for a single line instruction.

Thanks


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hello,

I can't
SyntaxError: lambda cannot contain assignment

Sorry, forgot that assignment is not an operation but considered a statement in python. You would need to have a function for setting the variable. If your class already has a setter for that variable you can call that in the lambda.

i.e)
lambda evt: self.SetAttr(None)

Also, even if you don't have a setter, you can use the python built-in setattr function ... i.e. for the given example

self.btn.Bind(wx.EVT_BUTTON, lambda evt: self.attr.value = None)

do:

self.btn.Bind(wx.EVT_BUTTON, lambda evt: setattr(self.attr, 'value', None))

--Grant

···

On 18 Aug, 2008, at 05:24, Cody Precord wrote:

On Aug 18, 2008, at 7:02 AM, Prashant Saxena wrote: