Make filter() and map() return function if iterable is not passed

One of my students gave me an interesting question about why `filter` and `map` do not return a reusable function in case the iterable is not passed. With the current design ``` In [1]: a = filter(lambda x: x > 60) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-ad0178d4fce0> in <module> ----> 1 a = filter(lambda x: x > 60) TypeError: filter expected 2 arguments, got 1 ``` this is fine as long as you need to use that filter for once. But if you want to reuse it you either need to create the iterator every time specifying the filter function, or save the function in a var and create the filter with that. I'm wondering why can't we create custom filters and mappers that are reusable. The result will look something like this: ```
Are there any drawbacks or limitations to this? Is is worth working on?

On Fri, Feb 12, 2021 at 1:01 AM Henry Harutyunyan <henryharutyunyan@gmail.com> wrote:
Why not simply define your function and give it a name? def above_sixty(x): return x > 60 That'd give you most of the same benefits. Making filter/map do something completely different if you omit an argument is a bug magnet. ChrisA

On Fri, Feb 12, 2021 at 1:01 AM Henry Harutyunyan <henryharutyunyan@gmail.com> wrote:
Why not simply define your function and give it a name? def above_sixty(x): return x > 60 That'd give you most of the same benefits. Making filter/map do something completely different if you omit an argument is a bug magnet. ChrisA
participants (3)
-
Chris Angelico
-
Henry Harutyunyan
-
永田大和