andro
andro

Reputation: 939

Tuple with named elements

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?

Upvotes: 0

Views: 460

Answers (2)

Alex Riley
Alex Riley

Reputation: 176770

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.

Upvotes: 4

Diogo Martins
Diogo Martins

Reputation: 937

According to the sys module documentation:

Static objects:

[...]

version_info -- version information as a named tuple

[...]

Upvotes: 1

Related Questions