Pavel Hanpari Pavel Hanpari - 1 year ago 138
Python Question

Type hints in namedtuple

Consider following piece of code:

from collections import namedtuple
point = namedtuple("Point", ("x:int", "y:int"))

The Code above is just a way to demonstrate as to what I am trying to achieve.
I would like to make
with type hints.

Do you know any elegant way how to achieve result as intended?

Answer Source

You can use typing.NamedTuple

From the docs

Typed version of namedtuple.

>>> import typing
>>> Point = typing.NamedTuple("Point", [('x', int), ('y', int)])

This is present only in Python 3.5 onwards

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download