namedtuple
is a data structure in the Python standard library, located in the collections
module. It is a factory function used to create tuples with field names, allowing tuple fields to be accessed like object properties without needing to use indexes.
Using namedtuple
makes it easy to define a tuple type with field names without manually writing a class. This is very useful when a lightweight data container is needed, but you still want to access the data like object properties.
Here is a simple example demonstrating how to use namedtuple
:
In the example above, we defined a namedtuple
named Point
with two fields x
and y
. Then, we created a Point
object p1
and accessed its values using field names.