Collection Functions
collections.namedtuple()
Generate a tuple subclass with named, read-only fields, normal tuple indexing and optional defaults for rightmost fields.
Formal Syntax & Structure
python syntaxpython
collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| typename | str | Name of the generated tuple subclass. |
| field_names | iterable[str] or str | Field names, either iterable or separated by spaces or commas. |
| rename | bool | Automatically rename invalid or duplicate fields when true. |
| defaults | iterable or None | Default values for the rightmost fields. |
| module | str or None | Module name assigned to the generated type. |
Return Value
type
The type result described above.
Working Example
Example Snippetpython
import collectionsPoint=collections.namedtuple("Point","x y")p=Point(3,4)print((p.x,p[1],tuple(p)))