collections.namedtuple: conflicting instances?
crebert at ucsd.edu
Thu Sep 23 19:40:12 CEST 2010
On Thu, Sep 23, 2010 at 9:28 AM, David A. Barrett <c_barret at qualcomm.com> wrote:
> 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?
Documentation for human readers (the .__name__ class attribute). As
your example shows, the name you pass in doesn't have to match the
name you assign the resulting class to and actually use (i.e. "X" vs.
"IX"); it's rather like how you were able to use "nt" instead of
> Is it
> an error to construct two distinct classes with the same value?
Of course not. Otherwise, classes would have to have *globally unique*
names, and stuff like the following wouldn't be possible (which would
'''Models a mechanical engineer.'''
'''Models a person who drives a train.'''
# Models a train company and associated repair yard.
> inconsistant constructions with the same name raise and exception?
Probably not (see answer to previous sentence); showing a warning
/might/ be appropriate.
More information about the Python-list