On 1 August 2017 at 23:43, Ned Batchelder <[email protected]> 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 | [email protected] | Brisbane, Australia
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/