I need to do something that is functionally equivalent to this:
for foo in foos:
bar = foo.get_bar()
# Do something with bar
My first instinct was to use map, but this did not work:
for bar in map(get_bar, foos):
# Do something with bar
Is what I’m trying to accomplish possible with map? Do I need to use a list comprehension instead? What is the most Pythonic idiom for this?
Either with
lambda:Or simply with instance method reference on your instance’s class:
As this was added from a comment, I would like to note that this requires the items of
foosto be instances ofFoo(i.e.all(isinstance(foo, Foo) for foo in foos)must be true) and not only as the other options do instances of classes with aget_barmethod. This alone might be reason enough to not include it here.Or with
methodcaller:Or with a generator expression: