None的id(内存地址)由何决定?单机器及跨进程是否会变更?
Great question—let’s unpack this with both Python-level behavior and the C underpinning of CPython, since that’s where the real action happens.
1. Python中None的id由什么决定?会变更吗?
First, let’s get the basics straight: None is a singleton in Python. In CPython (the most common Python implementation), None is represented by a global static C object that’s initialized once when the interpreter starts up.
同一进程内:id绝对不会变更
From the moment you launch your Python interpreter to the moment it exits, the_Py_NoneStruct(the C struct behind None) lives at the same virtual memory address. Callingid(None)will always return the same integer here—no exceptions, because the singleton is never recreated or moved in memory during the process’s lifetime.不同进程/不同机器:id几乎肯定不同
The id you see is a virtual memory address, not a physical one. Modern operating systems use Address Space Layout Randomization (ASLR) to randomize where a process’s memory regions are loaded. This means every time you start a new Python process, the virtual address assigned to the global_Py_NoneStructwill be different. On different machines, even without ASLR, system configurations (like memory layout, OS version, or Python build options) can lead to different virtual addresses.
2. 关于C全局变量地址的误解(回应juanpa的疑问)
The idea that "C global variables have the same address across interpreter instances/processes" is a common misconception—let’s clarify:
虚拟地址 vs 物理地址
When you print the address of a C global variable, you’re seeing its virtual address, which maps to a physical address via the OS’s memory management unit (MMU). Each process has its own separate virtual address space, so even if two processes have a global variable at the same virtual address, the physical memory they point to is completely separate.When addresses seem the same
In environments where ASLR is disabled (rare on modern systems, but possible in some testing/legacy setups), the OS uses a fixed virtual memory layout. In that case, the same global variable might end up at the same virtual address across different processes. But this is not the default behavior, and it’s a property of the OS, not the C language or Python interpreter.CPython’s case
In CPython,_Py_NoneStructis a global variable declared in the C source. When ASLR is enabled (the default on Windows, macOS, Linux), every new Python process will load this variable at a different virtual address—soid(None)will differ between runs. If you disable ASLR, you might see the same id across processes, but that’s an OS-level tweak, not a Python guarantee.
总结
- Same Python process:
id(None)is fixed forever—no way it changes. - Different processes/machines:
id(None)will almost always differ, thanks to ASLR and OS memory layout differences. - C global variable addresses across processes: Only the virtual address might match (without ASLR), but physical addresses are unique per process. ASLR makes even virtual addresses randomize by default.
内容的提问来源于stack exchange,提问作者JacobIRR

