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.

classification
Title: namedtuple vs tuple
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ezio.melotti, hkrishnan, rhettinger
Priority: low Keywords:

Created on 2010-04-16 04:28 by hkrishnan, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg103289 - (view) Author: H Krishnan (hkrishnan) Date: 2010-04-16 04:28
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.
msg103290 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-04-16 04:49
You should propose this on the python-ideas mailing list first.
Anyway it's already too late to do this on Python 2.x, and since it's not backward compatible for the 1-element case, it won't probably be accepted for 3.x either, so I suggest closing this as reject.
Adding Raymond to the nosy so he can take a decision.
msg103291 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-16 05:40
Sorry, this was by design.  There were two possible constructors, one using positional arguments or keywords and one from an iterable.  Either one could have been the main constructor.  A number of use cases favored the one that we choose.  It was a good decision, but not a perfect one.

To construct a namedtuple directly from an existing tuple, either use:
  ntuple(*t)
or
  ntuple._make(t)

The class is functioning as designed, documented, and tested.  There is no way to change it without breaking most existing uses.  Closing as rejected.
msg103553 - (view) Author: H Krishnan (hkrishnan) Date: 2010-04-19 05:08
Sorry, I didn't know about "python-ideas".
Actually, there is a way to do this without breaking any existing code.

namedtuple could support an optional additional argument, say, useIterableCtr, which is by default False, and the class template could be appropriately modified based on this argument.

I couldn't find the PEP for this, but I notice that other people have also suggested iterable as argument in the ActiveState recipe page for this.
msg103554 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-19 05:15
We don't do flags either :-)
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52662
2010-04-19 05:15:32rhettingersetmessages: + msg103554
2010-04-19 05:08:28hkrishnansetmessages: + msg103553
2010-04-16 05:40:29rhettingersetstatus: open -> closed
assignee: rhettinger
resolution: rejected
messages: + msg103291
2010-04-16 04:49:33ezio.melottisetpriority: low
versions: + Python 3.2, - Python 2.6
nosy: + rhettinger, ezio.melotti

messages: + msg103290

components: + Library (Lib), - None
2010-04-16 04:28:21hkrishnancreate