Value of Py_None

2024/10/9 2:29:16

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.

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?

Additionally, the documentation also points out:

Note that the PyTypeObject for None is not directly exposed in the Python/C API.

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.

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

Answer

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).

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.

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

EDIT: 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.

https://en.xdnf.cn/q/70068.html

Related Q&A

Getting the href of a tag which is in li

How to get the href of the all the tag that is under the class "Subforum" in the given code?<li class="subforum"> <a href="Link1">Link1 Text</a> </l…

Put value at centre of bins for histogram

I have the following code to plot a histogram. The values in time_new are the hours when something occurred.time_new=[9, 23, 19, 9, 1, 2, 19, 5, 4, 20, 23, 10, 20, 5, 21, 17, 4, 13, 8, 13, 6, 19, 9, 1…

plot in Pandas immediately closes

I have a problem of plotting the data. I run the following python code:import pandas as pd df = pd.read_csv("table.csv")values = df["blah"] values.plot() print 1df[blahblah].plot() …

Django template: Embed css from file

Im working on an email template, therefor I would like to embed a css file<head><style>{{ embed css/TEST.css content here }}</style> </head>instead of linking it<head><…

handling async streaming request in grpc python

I am trying to understand how to handle a grpc api with bidirectional streaming (using the Python API).Say I have the following simple server definition:syntax = "proto3"; package simple;serv…

Add new column to a HuggingFace dataset

In the dataset I have 5000000 rows, I would like to add a column called embeddings to my dataset. dataset = dataset.add_column(embeddings, embeddings) The variable embeddings is a numpy memmap array of…

Django: how to order_by on a related field of a related field

Im using annotate to add a property to an object which I can then use for order_by. However, I want to annotate on a field of a relation on a relation. I know I should be able to get to the field someh…

How to extract the cell state and hidden state from an RNN model in tensorflow?

I am new to TensorFlow and have difficulties understanding the RNN module. I am trying to extract hidden/cell states from an LSTM. For my code, I am using the implementation from https://github.com/ay…

Python - Nested List to Tab Delimited File?

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g.,nested_list = [[x, y, z], [a, b, c]].I wish to create a function in order to output this data construct into a tab delim…

How to make sure buildout doesnt use the already installed packages?

I am trying to switch fully to buildout - but our development environment already has lot of stuff installed in /usr/lib/pythonxx/How can I make sure that buildout doesnt use the libraries installed on…