[Python-ideas] "any" and "all" support multiple arguments
Nick Coghlan
ncoghlan at gmail.com
Tue Aug 1 11:16:02 EDT 2017
On 1 August 2017 at 23:43, Ned Batchelder <ned at nedbatchelder.com> wrote:
> I find it frustrating that they always return booleans. It would be more
> useful if any() returned the first true value it finds. This seems like a
> backward-compatible-enough change to me... :)
While I'm not sure how to interpret that smiley, I figure it's worth
making it explicit that this is decidedly *not* true given
type-dependent serialisation protocols like JSON:
>>> import json
>>> class MyClass:
... def __bool__(self):
... return True
...
>>> json.dumps(any([MyClass()]))
'true'
>>> json.dumps(MyClass())
Traceback (most recent call last):
...
TypeError: Object of type 'MyClass' is not JSON serializable
The idea of elevating first_true from its current status as an
itertools recipe to actually being an itertools module API has
certainly come up before, though.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list