On Mon, Oct 11, 2021 at 06:59:14PM -0400, Erik Demaine wrote:
> In the end, I feel like the main case I want to use a `first` and `last`
> functions on are `dict`s;
"I want to use first on dicts" is not really a use-case. Presumably
you're not just doing:
d = {key: value, ...}
who_cares = first(d)
del who_cares
process(d)
I assume you're not just extracting the first value for the LOLs, you
must have some reason for it. It is *that reason* which counts as a
use-case.
I think the fact that it seems hard to get a really compelling use-case
that isn't extremely niche does suggest that this doesn't need to be a
named function. We don't bloat classes and the builtins with named
trivial one-liners unless they are frequently useful, fundamental and
really compelling:
float.add_one() # return float + 1.0
float.double() # return float*2.0
list.length_equals_one() # return len(list) == 1
I think that even if there are occassional uses for first as an alias
for next(iter(dict)), it fails to be useful *enough*, fundamental or
compelling to justify bloating the API with such a simple one-liner.
If people disagree, you can argue by demonstrating good use-cases
(hopefully *common* use-cases), or by demonstrating that other languages
provide this functionality.
As for the argument that the idiom `next(iter(obj))` is "not intuitive",
true. Neither is `seq[0]`, or with statements, or range, or classes, or
importing, or async, or comprehensions, or regular expressions, or
pretty much everything else in Python. And yet somehow we cope.
It is okay to learn how do something.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/SAZLCIVFOFNZE3Q2K3TOYM6L2WU4DSTJ/
Code of Conduct: http://python.org/psf/codeofconduct/