0

In python 2.7.10, sys.version_info from the sys module is:

sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)

What python type is this? It appears to be some sort of a tuple with named elements, and you can refer to either

sys.version_info[0]

or

sys_version_info.major

The type command returns

<type 'sys.version_info'>

which is somewhat unhelpful. I know it is not a named tuple, and it's not a plain tuple, but what is it? How does one construct such an item?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
andro
  • 901
  • 9
  • 20
  • Please clarify - you know about collections.namedtuple already and want to know what sys.version_info is and how it differs from collections.namedtuple? – Hammerite Aug 26 '15 at 10:46
  • It's a [built-in type](https://github.com/python/cpython/blob/9907203a7b805630b1fe69e770164449b5d53cff/Python/sysmodule.c#L1551), I don't think you can create custom instances of it. – bereal Aug 26 '15 at 10:47
  • I was unable to find a description of what this object is in the 2.7 documentation for sys.version_info. I was puzzled as to how a tuple could have named elements, and was asking how to make a tuple like this. Now I know it is a built-in type, is there a name for this sort of thing? – andro Aug 26 '15 at 10:58

2 Answers2

4

sys.version_info is actually a C struct object defined in structseq.c. As the comment at the top of that code indicates, it's primarily intended as an implementation tool for modules:

/* Implementation helper: a struct that looks like a tuple.  See timemodule
   and posixmodule for example uses. */

If you want a similar Python object, this is a little bit like collections.namedtuple. In fact the doc string for sys.version_info uses "named tuple" to describe the object:

>>> print sys.version_info.__doc__
sys.version_info

Version information as a named tuple.

Alternatively, you might be able to use the structseq object directly at C level with the Python C API.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
1

According to the sys module documentation:

Static objects:

[...]

version_info -- version information as a named tuple

[...]

Diogo Martins
  • 917
  • 7
  • 15
  • I referred to 2.7 in my post. The 2.7 doc has only: Changed in version 2.7: Added named component attributes No mention of named tuples. – andro Aug 26 '15 at 11:15