Differences creating tuples and collections.namedtuples

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Feb 18 15:15:50 CET 2013


On 18 February 2013 14:09, John Reid <j.reid at mail.cryst.bbk.ac.uk> wrote:
> On 18/02/13 12:11, Dave Angel wrote:
>> On 02/18/2013 06:47 AM, John Reid wrote:
>>> Hi,
>>>
>>> I was hoping namedtuples could be used as replacements for tuples in
>>> all instances. There seem to be some differences between how tuples
>>> and namedtuples are created. For example with a tuple I can do:
>>>
>>> a=tuple([1,2,3])
>>>
>>> with namedtuples I get a TypeError:
>>>
>>> from collections import namedtuple
>>> B=namedtuple('B', 'x y z')
>>> b=B([1,2,3])
>>
>> You are passing a single list to the constructor, but you specified that
>> the namedtuple was to have 3 items.  So you need two more.
>
> I'm aware how to construct the namedtuple and the tuple. My point was
> that they use different syntaxes for the same operation and this seems
> to break ipython. I was wondering if this is a necessary design feature
> or perhaps just an oversight on the part of the namedtuple author or
> ipython developers.

I would say that depending on isinstance(obj, tuple) was the error. I
can't offer a suggestion as you haven't clarified what the purpose of
this code in canSequence() is or what constraints it is expected to
satisfy.


Oscar



More information about the Python-list mailing list