On Wed, Jul 01, 2020 at 02:12:35PM +0100, Stestagg wrote:
> > And you needed to be told several times before you learned that dicts,
> > and dict views, are not sequences?
>
> I've spent vastly more of my professional career developing in python
> environments where such code worked (python versions < 3).
You have never used a version of Python where you could index dicts or
dict views, it has never worked. We've been able to index *lists*, of
course, and the natural translation from Python 2:
mydict.keys()[0] # Indexing a *list* not a view
is to write this:
list(mydict.keys())[0]
If you want to emulate code from Python 2, that is what you should be
writing. Problem solved!
I've seen many changes in Python over the years, and sometimes it's
taken me a while to adapt. But when I keep getting the same error, I
take it as *my failure*, not a sign that the language is wrong and needs
to be reverted -- especially when there is a simple, and well-known,
work-around to give me the older semantics with a minimum of trouble.
So when I hear someone describing themselves as a *senior* Python dev,
but apparently being unable to adapt to the change of spelling
`.viewitems` --> `.items`, it takes me by surprise.
> The fact that
> one of the many types of ordered sequence containers in python doesn't
> support numeric indexing seems like a reasonable thing to forget,
Neither dicts nor dict views are sequences, so that is not a fact.
py> from collections.abc import Sequence
py> isinstance({}, Sequence)
False
py> isinstance({}.items(), Sequence)
False
They're not even conceptually sequences; they are conceptually mappings
and sets.
In Python 2.7, the same applied, correcting for the change of spelling:
py> from collections import Sequence
py> isinstance({}.viewitems(), Sequence)
False
Of course in 2.x `dict.items` returned a sequence (a list), which means
that it supported the full complement of list methods, such as sort,
reverse, slicing, insertion, appending etc. Being a list, it could
contain duplicates:
py> obj = {}.keys()
py> obj.extend([1, 1])
py> assert obj == [1, 1]
Should we push for dict views to support all the methods that lists
supported in Python 2, so that people don't have to learn the new
spelling? I don't think so.
> especially when such containers are a relatively new addition to the
> language, and are encountered infrequently. Your wording here sounds like
> this is something I should feel shame about?
Dict views have been in the language for a decade (to be precise: eleven
and a half years), so over one third of the existance of the language.
Anyone with less than a decades' experience in Python has never known a
time that Python didn't have dict views.
Python 3 `dict.items` is not the same thing as Python 2's `dict.items`,
it is Python 2.7's `dict.viewitems`. If you want a list, call `list`.
That is effectively what Python 2 was implicitly doing. Python 3 just
makes it explicit.
> Hopefully if this proposed change is made, then dict views, which are,
> after all, iterable ordered containers, can be considered Sequences again.
Dict views have never been considered sequences. This is not a reversion
to older semantics, this is an unprecedented new behaviour.
--
Steven
_______________________________________________
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/OZYHW4OJWAXR6C4NU5IMZQI3BBCMU5KQ/
Code of Conduct: http://python.org/psf/codeofconduct/