r/Python • Freelancer. AnyFactor.xyz • Sep 16 '20

News An update on Python 4

Post image
3.3k Upvotes

389 comments sorted by

View all comments

Show parent comments

-3

u/Leav Sep 16 '20

Because 3.10 = "3.1 with more precision" to engineers and scientists and basically all non-developer users.

How would python sort these lists: [3.8,3.9,3.10] or ["3.8","3.9","3.10"]?

14

u/Karnyl Sep 16 '20

If you used a library aware of how versions should be sorted, like semver or distutils.version, then they would be sorted correctly.

>>> from distutils.version import LooseVersion
>>> versions = ["3.8","3.9","3.10"]
>>> sorted(versions, key=LooseVersion)
"3.8","3.9","3.10"

4

u/flying-sheep Sep 16 '20

Please don't use distutils.version anymore, it doesn't follow PEP 440, which defines how Python package versions work. Use packaging.version instead.

0

u/Leav Sep 17 '20

Sure, but to newbies this is confusing and non intuitive, that's all we're saying here.

No one is saying this is wrong or should change.

7

u/netgu Sep 16 '20

This isn't about engineering or science so that point doesn't matter, honestly.

I realize that engineers/scientists will use python. When they do that they are functioning as developers/software engineers (those are just facts)and as such they should get comfortable dealing with the software development side of the software development they are doing.

There are rules about how these numbers work that mean the world to developers and software engineers (this is the place this matters and so these are the concerns that dictate how it is done) to the point that there is an entire standard built around it that will answer all your questions (including the ordering one):

https://semver.org/

1

u/Leav Sep 17 '20

Acceptable. I wasn't trying to say that the way versions work should change for the benefits of newcomers, just that it was confusing.

2

u/flying-sheep Sep 16 '20

None of these lists contain versions. Try this one:

from packaging.version import Version

versions = [Version(v) for v in ["3.8", "3.9", "3.10"]]
assert sorted(versions) == versions

For versions containing only numbers (and not e.g. 3.9b1) you can also think of them as tuples of integers.

0

u/Leav Sep 17 '20

Yes, I understand this is how versions work, but to someone who is not familiar with them, it looks extremely weird and confusing.

1

u/flying-sheep Sep 17 '20

Well, welcome to human language notation. There's things that look similar but aren't the same. Boohoo.

2

u/pepoluan Sep 17 '20

The problem is people treating "." as decimal point instead of "arbitrary separator that happens to be a period".

Limiting oneself to "3.9" is ambiguous. But how do you consider "3.9.1"? Or to use latest Python stable, "3.8.5"?

When facing version numbers, one must split on this "arbitrary separator that happens to be a period" first, integer-ize the portions, tuple-ify the result ... then perform a comparison.

Now to answer your question, "how to sort":

list_of_version_strings = ["3.10", "3.8", "3.9", "3.8.5"]
converter = lambda x: tuple(map(int, x.split(".")))
list_of_version_tuples = list(map(converter, list_of_version_strings))
sorted(list_of_version_tuples)

# Result is:
# [(3, 8), (3, 8, 5), (3, 9), (3, 10)]