Py_None 的价值
Value of Py_None
我很清楚 None
用于表示缺少值。但是由于在实施过程中一切都必须有一个潜在的价值,我想看看使用了什么价值来表示没有价值,关于 CPython
.
我明白了,基于 documentation, that NoneObject
is a singleton. Since my c
skills are rusty, my best, amateur guess, would be that the value of None
would be the pointer to the memory allocated for the Py_None
object; since it is a singleton this would guarantee uniqueness. Or is it assigned to c
's NULL
which has a value of 0x0000
based on the second answer in this question?
此外,文档还指出:
Note that the PyTypeObject for None is not directly exposed in the Python/C API.
我猜这意味着您无法通过源代码搜索找到它。 (我做了,不知道去哪里看,因为 object.c
天真地认为我能理解任何东西)
但是我不确定我对此的看法所以我问了。
CPython
中 Py_None
对象的 c
级别值是多少?
Py_None
是 Py_None
,如果没有其他值要返回,则必须在正常操作期间从函数中增加和返回。 NULL
仅在向 VM 发出异常信号时返回,实际异常对象为 created/assigned separately.
Py_None
是Include/object.h
中的宏定义。它是 object.c
中 _Py_NoneStruct
的别名,它是 PyObject
类型(结构)的静态(如在存储中)全局变量。它在 Python 项中被分配为 NoneType
(在 object.c
中定义在它的正上方并且仅在 _Py_NoneStruct
中使用一次)。
所以它不是 NULL 或 C 中的任何其他特殊值,它是 _PyNone_Type
的单例 PyObject
实例。至于 _PyNone_Type
PyTypeObject
没有被公开,我想他们指的是 static
关键字(即内部链接),这意味着 PyTypeObject
只能在 object.c
并且只在 PyNone
.
的定义中使用一次
只是补充一点,每当文档说 PyNone
没有类型时,不应按字面意思理解。它有一种特殊的类型,NoneType
,您仍然可以通过 None
单例访问它,但您不能创建新实例或做任何其他您可以用普通类型做的事情。对于不创建新实例似乎存在硬编码限制,虽然我无法在 CPython 源代码中找到它的确切定义位置,但您可以在尝试创建新实例时看到它的效果:
>>> type(None)
<type 'NoneType'>
>>> type(None)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'NoneType' instances
编辑:当 tp_new
字段为 NULL 时,似乎从 typeobject.c
抛出了错误。令人惊讶的是 _PyNone_Type
似乎是用非 NULL tp_new
定义的(指向 object.c
中的静态 none_new
)。之后的某个时候它可能会设置为 NULL,但这只是一个实现细节,对您的问题范围并没有真正的影响。
Py_None
是 _Py_NoneStruct
结构定义的地址值。
见the code:
/*
_Py_NoneStruct is an object of undefined type which can be used in contexts
where NULL (nil) is not suitable (since NULL often means 'error').
Don't forget to apply Py_INCREF() when returning this value!!!
*/
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
#define Py_None (&_Py_NoneStruct)
我很清楚 None
用于表示缺少值。但是由于在实施过程中一切都必须有一个潜在的价值,我想看看使用了什么价值来表示没有价值,关于 CPython
.
我明白了,基于 documentation, that NoneObject
is a singleton. Since my c
skills are rusty, my best, amateur guess, would be that the value of None
would be the pointer to the memory allocated for the Py_None
object; since it is a singleton this would guarantee uniqueness. Or is it assigned to c
's NULL
which has a value of 0x0000
based on the second answer in this question?
此外,文档还指出:
Note that the PyTypeObject for None is not directly exposed in the Python/C API.
我猜这意味着您无法通过源代码搜索找到它。 (我做了,不知道去哪里看,因为 object.c
天真地认为我能理解任何东西)
但是我不确定我对此的看法所以我问了。
CPython
中 Py_None
对象的 c
级别值是多少?
Py_None
是 Py_None
,如果没有其他值要返回,则必须在正常操作期间从函数中增加和返回。 NULL
仅在向 VM 发出异常信号时返回,实际异常对象为 created/assigned separately.
Py_None
是Include/object.h
中的宏定义。它是 object.c
中 _Py_NoneStruct
的别名,它是 PyObject
类型(结构)的静态(如在存储中)全局变量。它在 Python 项中被分配为 NoneType
(在 object.c
中定义在它的正上方并且仅在 _Py_NoneStruct
中使用一次)。
所以它不是 NULL 或 C 中的任何其他特殊值,它是 _PyNone_Type
的单例 PyObject
实例。至于 _PyNone_Type
PyTypeObject
没有被公开,我想他们指的是 static
关键字(即内部链接),这意味着 PyTypeObject
只能在 object.c
并且只在 PyNone
.
只是补充一点,每当文档说 PyNone
没有类型时,不应按字面意思理解。它有一种特殊的类型,NoneType
,您仍然可以通过 None
单例访问它,但您不能创建新实例或做任何其他您可以用普通类型做的事情。对于不创建新实例似乎存在硬编码限制,虽然我无法在 CPython 源代码中找到它的确切定义位置,但您可以在尝试创建新实例时看到它的效果:
>>> type(None)
<type 'NoneType'>
>>> type(None)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot create 'NoneType' instances
编辑:当 tp_new
字段为 NULL 时,似乎从 typeobject.c
抛出了错误。令人惊讶的是 _PyNone_Type
似乎是用非 NULL tp_new
定义的(指向 object.c
中的静态 none_new
)。之后的某个时候它可能会设置为 NULL,但这只是一个实现细节,对您的问题范围并没有真正的影响。
Py_None
是 _Py_NoneStruct
结构定义的地址值。
见the code:
/*
_Py_NoneStruct is an object of undefined type which can be used in contexts
where NULL (nil) is not suitable (since NULL often means 'error').
Don't forget to apply Py_INCREF() when returning this value!!!
*/
PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
#define Py_None (&_Py_NoneStruct)