✘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:
>
> https://docs.python.org/2.7/library/collections.html?highlight=namedtuples#collections.namedtuple
>
> namedtuples makes no mention of a limitation on the number of elements in
> a namedtuple.
>
> But:
>
> >>> 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...
Name: signature.asc
Type: application/pgp-signature
Size: 811 bytes
Desc: not available
URL: <http://lists.ntpsec.org/pipermail/devel/attachments/20160909/6e9b62a6/attachment.bin>
More information about the devel
mailing list