[Tutor] map() and lambda to change class instance attribute
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Wed May 11 20:01:23 CEST 2005
On Wed, 11 May 2005, Bernard Lebel wrote:
> Let say I have several class instances in a list, and these class
> instances have an attribute named "value", whose value is an integer.
>
> I would like to know if it is possible to loop over the list of
> instances to change their "value" attribute, using a map( ( lambda:...),
> ... ) type of loop.
Hi Bernard,
Hmmm... then map() is probably not what you want. map() is meant to
transform a list of things into another list of things, but isn't really
meant to mutate its input.
It is possible to abuse map() to do what you're trying to do, using the
setattr() function:
###### Pseudocode
map(lambda instance: setattr(instance, 'value', 42))
######
but this is definitely language abuse. *grin*
map() is not intended to be run just to affect changing assignments on its
input. It'll probably be clearest in Python just to write out the loop
explicitely.
Best of wishes to you!
More information about the Tutor
mailing list