You do not specify a name twice. You specify one "internal" name when calling namedtuple, and then you assign the resulting namedtuple type to a variable.
The one you specify as an argument for namedtuple is the resulting type namedtuple, the native type of its name, that is, "what it calls." The thing to the left of the equal sign is a regular Python variable to which you assign the namedtuple type.
You can only use the created namedtuple if you assign it, and you can only use it by the name (s) that you assigned to it. Passing "OtherTypeName" as the "name" does not magically create a variable called OtherTypeName , so you are trying to use NameError when trying to use the name OtherTypeName . The only real use of the name passed in namedtuple ("OtherTypeName" in your case) is to display the resulting values.
Obviously, in many cases it is useful to have a variable that you use to refer to namedtuple, just like your own internal name; it makes things less confusing. But you can have several variables pointing to the same namedtuple:
NameOne = namedtuple('OtherTypeName', ['item']) NameTwo = NameOne
., or there are no variables pointing directly to it, and access it only through some container:
x = [] x.append(namedtuple('OtherTypeName', ['item'])) x[0]
This is not so much that there are special โapplicationsโ for this, since the behavior itself is not special: namedtuple is an object like any other, and creating an object is not the same as creating a variable that refers to it.
source share