0
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=11, releaselevel='final', serial=0)
>>> ver = sys.version_info
>>> ver > (3, 7, 11)
True
>>> ver > (3, 7, 12)
False
>>> ver == (3, 7, 11)
False
>>> ver == (3, 7, 10)
False
>>> ver == (3, 7, 12)
False

I don't know why ver > (3, 7, 11) returns true even they are the same? Also how do I test if versions are equal given ver == (3, 7, 11) returns False

James Lin
  • 25,028
  • 36
  • 133
  • 233
  • 1
    Related: https://stackoverflow.com/questions/11887762/how-do-i-compare-version-numbers-in-python – jarmod Aug 30 '21 at 01:40
  • 1
    `sys.version_info` behaves like a named tuple. `(3, 7, 11, 'final', 0) > (3, 7, 11)` is true, so the comparison you're performing is true as well. – user2357112 Aug 30 '21 at 01:43
  • 1
    Because `sys.version_info` is not a 3-tuple. It contains more information than the tuple you compare it to. Try something like `sys.version_info[:3] == (3, 7, 11)`. A safer choice is `(sys.version_info.major, sys.version_info.minor, sys.version_info.micro)`. – Selcuk Aug 30 '21 at 01:43
  • `sys.version` is a `namedtuple`, a subclass of `tuple`, which follows the standard lexicographical order defined [here](https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types): "If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one." – blhsing Aug 30 '21 at 01:44
  • You can do ```ver=(ver.major,ver.minor,ver.micro)``` –  Aug 30 '21 at 01:44
  • What you see if you do this? >>> (3, 8, 2, 1) > (3, 8, 2) True – Daniel Hao Aug 30 '21 at 01:45
  • cool thanks guys, I thought the `version_info` would have its own `__eq__` implementation, but seems not. – James Lin Aug 30 '21 at 01:45
  • It does, but only when you compare it to other `sys.version_info` instances. – Selcuk Aug 30 '21 at 01:46

0 Answers0