[Python-ideas] custom predicate for all() & any()

Franklin? Lee leewangzhong+python at gmail.com
Sat Mar 26 19:01:12 EDT 2016


Others have already demonstrated using generators and `map` for the general
case, but FYI, the following are equivalent:
1. `all(iterable)`
2. `all(map(bool, iterable))`

Because these are equivalent:
1. `if obj:`

2. `if bool(obj):`
On Mar 26, 2016 12:31 PM, "titouan dk" <dk.titouan at gmail.com> wrote:

> Hi,
>
> I was wondering if the builtins functions all() & any() could ever accept
> a custom predicate:
>
>     all(iterable, predicate=bool)
>     any(iterable, predicate=bool)
>
> Something like Lodash.js functions: every(), ...
>
> ## Example 1
>
>     ages = [21, 13, 18]
>     major = 18..__le__
>
>     allowed = all(ages, major)
>
> ## Example 2
>
>     people = [
>         {"name": "Mike", "age": 42},
>         {"name": "Josh", "age": 17}
>     ]
>     major = 18..__le__
>
>     allowed = all(people, {"age": major})
>
> ## Consistency drawbacks
>
> The filter() function takes its arguments in the reverse order:
>
>     filter(predicate_or_none, iterable)
>
> I would have write this function like this...
>
>     filter(iterable, predicate=None)
>
> ... but this is not the case, anyway.
>
> ## Your opinion?
>
> What do you think about this?
> Good idea, could be a quick-win?
> Or requiring a lot of efforts for nothing?
> What about consistency drawbacks?
>
>
> Thanks for reading,
>
> Titouan de Kerautem
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160326/4ebc5c5d/attachment-0001.html>


More information about the Python-ideas mailing list