Message194343
It's a direct counterpart to any() and all() - first([0, [], ()]) is None for the same reason that any([0, [], ()]) and all([0, [], ()]) are both False.
If first returned the actual first item in the iterable (regardless of truth value), then it would just be "next" under a different name, which would be rather pointless.
That said, if "first" is deemed too ambiguous, then I believe "filterednext" would be a reasonable more explicit name:
>>> filterednext([0, None, False, [], (), 42])
42
>>> filterednext([0, None, False, [], ()], default=42)
42
>>> filterednext([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
4
>>> m = filterednext(regexp.match('abc') for regexp in [re1, re2])
I also believe itertools would be a more appropriate initial home than the builtins. |
|
Date |
User |
Action |
Args |
2013-08-04 10:58:38 | ncoghlan | set | recipients:
+ ncoghlan, rhettinger, lukasz.langa, hynek, serhiy.storchaka |
2013-08-04 10:58:38 | ncoghlan | set | messageid: <1375613918.37.0.222692945276.issue18652@psf.upfronthosting.co.za> |
2013-08-04 10:58:38 | ncoghlan | link | issue18652 messages |
2013-08-04 10:58:38 | ncoghlan | create | |
|