This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: String comparison with dotted numerical values wrong
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: PedanticHacker, gvanrossum
Priority: normal Keywords:

Created on 2020-03-18 17:25 by PedanticHacker, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg364535 - (view) Author: Boštjan Mejak (PedanticHacker) * Date: 2020-03-18 17:25
I stumbled upon a possible bug in the Python interpreter while doing some Python version comparisons.

Look at this:
"3.10.2" < "3.8.2"
True  # This is not true as a version number comparison

Now look at this:
"3.10.2" < "3.08.2"
False  # Adding a leading 0 compares those two version numbers correctly

Is it possible Python is fixed to correctly compare such numbers? That would make comparing Python version numbers possible in the future.

import platform
if platform.python_version() < "3.8.2":
    # Do something

This is currently possible and correct, but this will break when Python version number becomes 3.10 and you wanna compare this version number to, say, 3.9.
msg364537 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-03-18 17:29
That's why for over a decade we've been recommending not to use string comparisons to compare versions. You have to parse the version and then compare the numeric values.
msg364539 - (view) Author: Boštjan Mejak (PedanticHacker) * Date: 2020-03-18 17:33
What is then the most Pythonic way of comparing two version numbers?
msg364540 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-03-18 17:34
That's a question for a user forum. There's some code in Lib/distutils/version.py.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84185
2020-03-18 17:34:56gvanrossumsetmessages: + msg364540
2020-03-18 17:33:20PedanticHackersetmessages: + msg364539
2020-03-18 17:29:19gvanrossumsetstatus: open -> closed
resolution: not a bug
messages: + msg364537

stage: resolved
2020-03-18 17:25:19PedanticHackercreate