✘namedtuples overly limited
Eric S. Raymond
esr at thyrsus.com
Fri Sep 9 05:43:47 UTC 2016
Gary E. Miller <gem at rellim.com>:
> Yo All!
> 2nd Python undocumented limitation:
> namedtuples makes no mention of a limitation on the number of elements in
> a namedtuple.
> >>> a = collections.namedtuple('a', 'b', 'c', 'd', 'e')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: namedtuple() takes at most 4 arguments (5 given)
Your invocation syntax is wrong. You failed to notice that arg 2 is a list.
The correct way to do what you want is
a = collections.namedtuple("mytype", ['a', 'b', 'c', 'd', 'e'])
giving the tuple field names as a list of strings. The value of a will be
the new lightweight class object with the name "mytype".
The argument prototype on that page was trying to clue you in. Look
where it says:
collections.namedtuple(typename, field_names[, verbose=False][, rename=False])
Four formal args, two optional, and the second formal is a plural.
Good job spotting this feature, though. I only learned of it last week.
It's a real aid to readability.
<a href="http://www.catb.org/~esr/">Eric S. Raymond</a>
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 811 bytes
Desc: not available
More information about the devel