python - 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
解决方案
"Name" is an overloaded term in Python. When some refers to the "name of a type", it could be one of two things:
The "inherent" name, which is part of the definition of the type itself and accessible via its
__name__
attribute.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
.
推荐阅读
- c# - Unity - 如果与子对象发生碰撞,如何锚定到对象
- python - 定义k-1个簇质心——SKlearn KMeans
- python - 位置参数跟随关键字 - menubar.add_cascade(label = "File", menu.filemenu) - 计算器
- javascript - TypeError:records.map 不是反应中的函数
- php - PHP - 将多个数组相互组合
- docusignapi - DocuSign Click API - 未经授权的响应调用 POST 协议
- next.js - 如何更改资产、图像等的基本路径
- rust - 将 Ref 返回到 Rc 内部的东西
> 没有 Ref::map - postgresql - Grafana Postgres 使用可具有多个值的 Where 查询时出错
- javascript - AWS Secrets Manager 和 javascript