On 03.03.2017 19:29, Ed Kellett wrote:
The reasons already stated boil down to "lists aren't dicts so they
shouldn't share methods", which seems ill-advised at best, and "I
wouldn't use this".
I wonder if those arguing against it also think dicts should not have
item access:
a[0]
dict or list? Why should it matter?
a.get(0, 'oops')
Doesn't look so different to me.
I'm not convinced that the latter is generally true; I've often looked
for something like a list.get, been frustrated, and used one (chosen
pretty much at random) of the ugly hacks presented in this thread. I'd
be surprised if I'm the only one.
You are not the only one. I share your sentiment.
I guess I don't have any hope of convincing people who think there's
no need to ever do this, but I have a couple of questions for the
people who think the existing solutions are fine:
- Which of the existing things (slice + [default], conditional on a
slice, conditional on a len() call) do you think is the obvious way to
do it?
None of them are. Try/except is the most obvious way. But it's tedious.
- Are there any examples where list.get would be applicable and not
the obviously best way to do it?
I don't think so. I already have given many examples/ideas of when I
would love to have had this ability. Let me re-state those and more:
- refactoring (dicts <-> lists and their comprehension counterparts)
- error-free accessing list comprehensions
- duck typing
- increased consistency of item access between dicts and lists
- the one obvious way to do it
- easier to teach
Sven
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/