0

Python 3 map returns a generator. But I want to apply my function immediately without going through generator. My code is:

printme = lambda x: print(x)

list(map(printme, fields))
for _ in map(printme, fields): pass

Why I have to build list or dive into loop? Why I can't just write map(printme, fields)? This idiom was working well for Python 2. What I have missed? Or how I can achieve this goal another way?

Most Wanted
  • 6,254
  • 5
  • 53
  • 70
  • 2
    It worked in Python 2, but it was never an idiom. –  Oct 01 '14 at 19:50
  • 2
    `map` is all about collecting return values. You're using it for side-effects rather than return values, which is non-standard use. I suggest writing your own map replacement that reflects your intent. Something like `def apply_to_all(func, iterable):for item in iterable:func(item)`. – Steven Rumbalski Oct 01 '14 at 20:00
  • Now that print is a function, print(*fields) works to do what I believe map(printme, fields) did, and it does not require a def or lambda. – Terry Jan Reedy Oct 02 '14 at 05:12

0 Answers0