1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
.. _sec.getitem.parts:
Accessing Parts Through Index Numbers
=====================================
.. versionadded:: 2.10.0
Another way to access parts of a version is to use an index notation. The underlying
:class:`~semver.version.Version` object allows to access its data through
the magic method :meth:`~semver.version.Version.__getitem__`.
For example, the ``major`` part can be accessed by index number 0 (zero).
Likewise the other parts:
.. code-block:: python
>>> ver = Version.parse("10.3.2-pre.5+build.10")
>>> ver[0], ver[1], ver[2], ver[3], ver[4]
(10, 3, 2, 'pre.5', 'build.10')
If you need more than one part at the same time, use the slice notation:
.. code-block:: python
>>> ver[0:3]
(10, 3, 2)
Or, as an alternative, you can pass a :func:`slice` object:
.. code-block:: python
>>> sl = slice(0,3)
>>> ver[sl]
(10, 3, 2)
Negative numbers or undefined parts raise an :py:exc:`python:IndexError` exception:
.. code-block:: python
>>> ver = Version.parse("10.3.2")
>>> ver[3]
Traceback (most recent call last):
...
IndexError: Version part undefined
>>> ver[-2]
Traceback (most recent call last):
...
IndexError: Version index cannot be negative
|