collections.namedtuple: conflicting instances?

David A. Barrett c_barret at qualcomm.com
Thu Sep 23 18:28:15 CEST 2010


  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?



More information about the Python-list mailing list