DEV Community

Discussion on: Python map() function

Collapse
 
xtofl profile image
xtofl

Nice! Higher order functions make for very expressive code with less clutter.

Did you know that a constructor can be used as well? For conversion between plain str arguments and actualPath for example:


paths = map(Path, sys.args)
assert all(map(Path.exist, paths))
Enter fullscreen mode Exit fullscreen mode

Caveat, though: map returns a generator, so it can be iterated only once. I often solve this with storing the result in a tuple:


paths = tuple(map(Path, sys.args))
assert all(map(Path.exist, paths))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Hi @xtofl ,

I didn't know this yet!
Thanks for the examples, written it down to look at I'm sure this can solve certain issues perfectly 😀