collections.namedtuple: conflicting instances?

D

David A. Barrett

I've noticed that it's possible to create conflicting instances of the
collections.namedtuple class:

from collections import namedtuple as nt
IX = nt('X', 'a b')
IY = nt('Y', 'c d')
x = IX(0, 1)
y = IY(2, 3)

The above are non-conflicting class instances and of two distinct
namedtuple classes and distinct instances of those classes, but what
happens with this?

IX2 = nt('X', 'g')
z = IX2(10)

It looks like IX and IX2 are two distinct classes, which makes sense,
but what is the classname parameter passed to the constructor used for?
Is it an error to construct two distinct classes with the same value?

I was wondering if it's useful to allow duplicate (consistant)
constructions, but have them simply return the same class instance.
Should inconsistant constructions with the same name raise and exception?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top