[Tutor] map() and lambda to change class instance attribute (fwd)

Alan Gauld alan.gauld at freenet.co.uk
Fri May 13 08:52:05 CEST 2005


> now I always used map() to perform a looped call on a function that
> would change the attribute value, as shown in Mark Lutz & David
> Ascher's Learning Python:
>
> # Perform attribute value change on a single instance
> def iterateInstances( oInstance ):
>       oInstance.value = myValue
>
> # Loop over list of instances
> map( iterateInstances, aListOfInstances )

How bizarre. I'm astonished that Lutz/Ascher even show that as a means
of changing an attribute. As Danny said, it's definitely an abuse of
map,
and there is no advantage whatsoever, as far as I can see. In fact I
suspect that it's actually slower than an explicit loop (because of
the extra list building).

> However I was looking into lambdas in hope to eliminate the need to
> define a function.

And a loop also avoids the need for a function, and therefore for
a function call, which is another reason the map should be slower.

Its completely weird that a text book should even suggest that map
be used for this!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list