On Mon, Jun 14, 2010 at 4:06 PM, Craig Yoshioka <crai...@me.com> wrote: > def makeStatus(definitions): > class Status(object): > pass > for key,function,data in definitions: > setattr(Status,key,property(lambda x: function(data))) > return Status() > > but all my properties now act as if they were invoked with the same data even > though each one should have been a new lambda function with it's own > associated data. It seems Python is 'optimizing'? all the lambdas to the > same object even though that's clearly not what I want to do. Anyone have > any suggestions as to: > > 1) why
Because the 'data' variable isn't local to the lambda function, so when it's called it looks it up in the outer (makeStatus) scope, where the current value assigned is whatever the last value was when the loop ran. > 2) what I should do Make it local to the lambda function: setattr(Status, key, property(lambda x, data=data: function(data))) Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list