collections.namedtuple

suggest change

Define a new type Person using namedtuple like this:

Person = namedtuple('Person', ['age', 'height', 'name'])

The second argument is the list of attributes that the tuple will have. You can list these attributes also as either space or comma separated string:

Person = namedtuple('Person', 'age, height, name')

or

Person = namedtuple('Person', 'age height name')

Once defined, a named tuple can be instantiated by calling the object with the necessary parameters, e.g.:

dave = Person(30, 178, 'Dave')

Named arguments can also be used:

jack = Person(age=30, height=178, name='Jack S.')

Now you can access the attributes of the namedtuple:

print(jack.age)  # 30
print(jack.name)  # 'Jack S.'

The first argument to the namedtuple constructor (in our example 'Person') is the typename. It is typical to use the same word for the constructor and the typename, but they can be different:

Human = namedtuple('Person',  'age, height, name')
dave = Human(30, 178, 'Dave')
print(dave)  # yields: Person(age=30, height=178, name='Dave')

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Collections module:
* collections.namedtuple

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
32 Collections module
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve