This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hkrishnan
Recipients hkrishnan
Date 2010-04-16.04:28:12
SpamBayes Score 9.053869e-14
Marked as misclassified No
Message-id <1271392103.11.0.515361003447.issue8415@psf.upfronthosting.co.za>
In-reply-to
Content
Named tuples and tuples have different creation behavior. Changing a tuple to a namedtuple will involve changing the usage as well. For example:

>>> ntuple = collections.namedtuple("ntuple", "a,b")
>>> ntuple(1,2)
ntuple(a=1, b=2)
>>> tuple(1,2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: tuple() takes at most 1 argument (2 given)
>>> tuple([1,2])
(1, 2)
>>> ntuple([1,2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() takes exactly 3 arguments (2 given)
>>>

Because of this, to create a tuple object given a 'tuple class', we need to do something like:
def makeTuple(tupleCls, *args):
   if hasattr(tupleCls, "_fields"):
      return tupleCls(*args)
   else:
      return tupleCls(args)

My suggestion: A namedtuple should also accept a single iterable as argument, in which case, the iterable will be broken up and assigned to individual fields.
This will break an existing behaviour of namedtuple: if only one field is present in the namedtuple and an iterable is passed to the namedtuple, that field is currently assigned the iterable. However, namedtuples are seldom used for single fields and so this may not be that important.
History
Date User Action Args
2010-04-16 04:28:24hkrishnansetrecipients: + hkrishnan
2010-04-16 04:28:23hkrishnansetmessageid: <1271392103.11.0.515361003447.issue8415@psf.upfronthosting.co.za>
2010-04-16 04:28:20hkrishnanlinkissue8415 messages
2010-04-16 04:28:13hkrishnancreate