In Python, is there a way to call a method on every item of an iterable? [closed]
- by Thane Brimhall
Possible Duplicate:
Is there a map without result in python?
I often come to a situation in my programs when I want to quickly/efficiently call an in-place method on each of the items contained by an iterable. (Quickly meaning the overhead of a for loop is unacceptable). A good example would be a list of sprites when I want to call draw() on each of the Sprite objects.
I know I can do something like this:
[sprite.draw() for sprite in sprite_list]
But I feel like the list comprehension is misused since I'm not using the returned list. The same goes for the map function. Stone me for premature optimization, but I also don't want the overhead of the return value.
What I want to know is if there's a method in Python that lets me do what I just explained, perhaps like the hypothetical function I suggest below:
do_all(sprite_list, draw)