本文介绍了Py_None的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

对我来说很明显,用于表示缺少值。但是,由于在实现过程中所有内容都必须具有基础值,因此我希望了解使用什么值来表示不存在值,关于 CPython

It is clear to me that None is used to signify the lack of a value. But since everything must have an underlying value during implementation, I'm looking to see what value has been used in order to signify the absence of a value, regarding CPython.

根据,即 NoneObject 是单例。由于我的 c 技能很生锈,所以我最好的业余猜测是, None 的值将是指针分配给 Py_None 对象的内存;由于它是单例,因此可以保证唯一性。还是将其分配给 c NULL ,其值为 0x0000 基于?

I understand, based on the 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?

此外,文档还指出:

我猜的意思是您找不到通过源搜索它。 (我这样做的原因是天真的相信我什么都可以理解)

Which I'm guessing means you cannot find it searching through source. (Which I did, not knowing where to look, for object.c naively believing I could understand anything)

但是我不确定我对此有何看法我问。

But I'm not certain about my opinion on this so I asked.

Py_None c 级别值是多少 CPython 中的$ c>对象?

What is the c level value for the Py_None object in CPython?

推荐答案

Py_None Include / object.h 中的宏定义。它是 object.c _Py_NoneStruct 的别名,它是 PyObject 类型(它是一个结构)。它以Python术语分配为 NoneType (在 object.c 的正上方定义,并且仅用于 _Py_NoneStruct )。

Py_None is a macro definition in Include/object.h. It is a an alias for _Py_NoneStruct in object.c which is a static (as in storage) global variable of PyObject type (which is a struct). It is assigned in Python terms to be of NoneType (defined right above it in object.c and only used once for _Py_NoneStruct).

因此它不是NULL或C中的任何其他特殊值,它是一个单例<$ c $ _PyNone_Type 的c> PyObject 实例。至于 _PyNone_Type PyTypeObject 没有公开,我想他们指的是静态关键字(即内部链接),这意味着 PyTypeObject 仅可在 object.c 和仅用于 PyNone 的定义。

So it's not NULL or any other special value in C, it's a singleton PyObject instance of _PyNone_Type. As for the _PyNone_Type PyTypeObject not being exposed, I suppose they refer to the static keyword (i.e. internal linkage) which means that the PyTypeObject is only accessible within object.c and is only used once for the definition of PyNone.

只要文档说,只需添加一点 PyNone 没有类型,因此不应从字面上理解。它具有一种特殊的类型, NoneType ,您仍然可以通过 None 单例访问该类型,但不能创建新实例或执行普通类型可以执行的其他任何操作。不创建新实例似乎有一个硬编码的限制,尽管我找不到CPython源代码中确切定义的位置,但是在尝试创建新实例时可以看到其效果:

Just to add to this a bit, whenever the documentation says that PyNone has no type, it should not be taken literally. It has a special kind of type, NoneType, which you can still access through the None singleton but you can't create new instances or do any other thing you can do with a normal type. There seems to be a hard-coded limitation for not creating new instances, and although I can't find exactly where it's defined in the CPython source you can see its effect when trying to create a new instance:

>>> 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,但这只是实现细节,并不能真正改变您的问题范围。

It seems that the error is thrown from typeobject.c when the tp_new field is NULL. Surprisingly though _PyNone_Type seems to be defined with a non-NULL tp_new (points to the static none_new in object.c). It might be set to NULL afterwards at some point, but it's just an implementation detail and doesn't really make a difference for the scope of your question.

这篇关于Py_None的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:24