首页 > 解决方案 > Python: Reason for the name parameter when defining a type or named tuple

问题描述

I'm still new to Python, so this may be a dumb question, but I don't understand the purpose of the first parameter to type() and namedtuple():

tuple_typename = namedtuple( 'tuple_typename', 'member1 member2 member3' )

custom_typename = type( 'custom_typename', (base_type,), members_dictionary )

I don't understand the difference between the return value and the string parameter? If the return value is a type reference, why do we need to specify it as a parameter? Or if we need to specify it as a string parameter, why do we need to capture the return value?

And one other related question: If the input string is a type-name specification, is there any way to control the namespace that type-name will be added to? For example, can I add a sub-class to an existing class from outside of that class?

Edit: Even though I accepted a good answer, I was wondering if anyone could clear one other thing up. Once I define a named tuple or custom type, when and/or where would I make use of the first string parameter? How can I reference the type-name later on using the name I specified for that parameter? Is that possible? Or is the type-reference lost once the function return value falls out of scope? Update: See comments of accepted answer

标签: pythontuplesnamedtuple

解决方案


"Name" is an overloaded term in Python. When some refers to the "name of a type", it could be one of two things:

  1. The "inherent" name, which is part of the definition of the type itself and accessible via its __name__ attribute.

  2. The name of a variable which refers to the type. There can be multiple variables that reference the same type, and none of these names are accessible from the type itself.

For example:

>>> class A:
...     pass
...
>>> x = A
>>> del A
>>> y = x
>>> A
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>> x.__name__
'A'
>>> y.__name__
'A'

The class statement sets both types of names: the identifier following the class keyword is used both to set the inherent name of the type (by using it as the first argument to the call to type) and to create a new variable that refers to the type. That variable, like any other, can be unset using del, but as long as other names still refer to the type, it is not lost. You can see the inherent name via either of the two extant references to type A.


推荐阅读