
I think one function returning multiple types must be confusing. You should use `functools.partial` or create new function with def or lambda.
from functools import partial
pass_filter = partial(filter, lambda x: x > 60) foo = [42, 56, 67, 87] for i in pass_filter(foo): print(i)
# 67 # 87
2021/02/11 23:04、Henry Harutyunyan <henryharutyunyan@gmail.com>のメール:
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:
```
pass_filter = filter(lambda x: x > 60) foo = [42, 56, 67, 87] for i in pass_filter(foo): print(i) 67 86 bar = {45, 94, 65, 3} for i in pass_filter(bar): print(i) 65 94
Are there any drawbacks or limitations to this? Is is worth working on? _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/IDSPO3VQWDANWON4LAEGZPNI4MDAZZBH/ Code of Conduct: http://python.org/psf/codeofconduct/