[Tutor] How can one know if a Python behavior is meant to be a maintained feature or just an implementation artifact?
Mats Wichmann
mats at wichmann.us
Fri Oct 25 14:52:55 EDT 2019
On 10/25/19 12:42 PM, boB Stepp wrote:
> I was at https://realpython.com/python-zip-function/ and read, "In
> Python 3.6 and beyond, dictionaries are ordered collections, meaning
> they keep their elements in the same order in which they were
> introduced. If you take advantage of this feature, then you can use
> the Python zip() function to iterate through multiple dictionaries in
> a safe and coherent way: ..." This immediately raised the question in
> my mind: How can I _know_ that I can rely on dictionary elements
> being ordered in all future versions of Python >= 3.6? And more
> generally, how can I tell when a consistent behavior is simply an
> implementation artifact or if it is an actual feature meant to be
> maintained in all future Python versions?
The "implementation detail" was made a language feature in 3.7, so the
documentation now says it's a guaranteed feature.
If you want to be more sure you can use an OrderedDict - that works for
all versions.
Normally the official documentation will take pains to tell you - if
some behavior should not be counted on; and for that which can be
counted on, in which Python version it became official. Here's an
example which is really old:
https://docs.python.org/3/library/functions.html#id
It may mean you need to check the docs for several versions, however.
python.org docs have a drop-down to select the version, but only
includes those versions that are considered "current".
More information about the Tutor
mailing list