Windows x64 上的 Python 2.* x86 (ie32bit) id() 函数可以返回的最大数字是多少?
Python 2.6 的 x86 的最高数字是多少 id()
< /a> 函数可以返回吗?
我认为上限必须是任何 32 位应用程序可以看到的内存量,必须是:232,即 4294967296?
(这很好,因为我必须将此值放入 C# 类型中,UInt32
或符合 CLS 的 Int64
就足够了,这就是我担心的原因,虽然与问题无关。)
但是如果我在 Windows x64 上运行,内存超过 2GB,比如 32GB 内存,Python 是否可能id()
函数,即使 Python 解释器本身是 x86,返回的值也高于 232???
我猜测这取决于 Python 解释器对机器的看法 - 我想象 WoW64 将 64 位内存地址转换为 32 位地址 - 但我只是猜测 - 我需要确定!
What is the highest number Python 2.6's x86 id()
function can return?
I presume the ceiling must be the amount of memory any 32-bit application can see which must be: 232, i.e. 4294967296?
(Which would be fine as I have to fit this value in a C# type for which UInt32
or the CLS-compliant Int64
would suffice, which it the reason for my concern, though irrelevant to the question.)
But what if a I am running on Windows x64 with more than 2GB of memory say 32GB of memory - is it possible for Python's id()
function, even though the Python interpreter itself is x86, to return a value higher then 232???
I am guessing it comes down to the Python interpreter's view of the machine - I imagine WoW64 translates 64-bit memory addresses to 32-bit addresses - but I am only guessing - I need to be sure!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从链接文档:
在 amd64 上的 Python 2.x 中,长整数是大于 64 位的整数。无论如何,在Python中,整数是无界的。因此,
id
可以返回任意长的值。如果您必须知道一个明确的最大值,您可以假设您的平台上的可用内存是您获得的整数大小的上限。因此,我为 32 位架构指定 2232,为 64 位架构指定 2264。因此,在 x86 python 实现的情况下,可以合理地将上限设置为 2232。
cpython(最流行的 python 实现)确实会返回内存地址(
Python/bltinmodule.c
中的builtin_id
):这将是一个 32 位/64 位值,但是正如文档中明确指出的,该行为是一个实现细节。根据定义,程序员不得依赖实现细节。
我强烈怀疑是否存在使用 ID 的合法用例,更不用说将它们转移到另一个程序了。相反,您应该使用自定义对象表,或者只传输一组。如果您打算将 Python 与 C# 结合使用,ironpython 允许您在相同的代码中执行此操作。
From the linked documentation:
In Python 2.x on amd64, a long integer is an integer larger than 64 bits. In any case, in Python, integers are unbounded. Therefore,
id
can return arbitrarily long values.If you must know a definite maximum value, you may assume that the available memory on your platform is an upper boundary for the size of the integer you get. Therefore, I'd specify 2232 for 32 bit, and 2264 for 64 architectures. In the case of an x86 python implementation, one can therefore place an upper boundary at 2232 with reasonable confidence.
cpython (the most popular python implementation) will indeed return a memory addresses (
builtin_id
inPython/bltinmodule.c
):This will be a 32 bit/64 bit value, but the behavior is an implementation detail, as explicitly stated in the docs. By definition, a programmer must not rely on implementation details.
I strongly doubt that there is a legitimate use case for ever using IDs, much less transferring them to another program. Instead, you should use a custom object table, or just transfer a set. If you intend to use Python in conjunction with C#, ironpython allows you to do so in the same code.
我认为实际上 id() 可以达到的最大值是 2**32 但实际上它永远不会达到这么高的数字。我在32位架构下尝试了上面的代码。如果我在理解您的问题时犯了任何错误,请告诉我。
4294967296L 是 2^32,我达到的最大 id() 是 1460876200。
i think in practical the max value the id() can achieve is 2**32 but in reality it never reaches such high number. I tried the above code in the 32 bit architecture . please let me know if i made any mistake in understanding your question.
4294967296L is the 2^32 and max id() i reached is 1460876200.
假设 id() 的文档正确并且 CPython (x86) 返回对象的地址,则可以返回的最大值为 232-1 (4294967295) 。这可以在一个简单的 C 程序中进行演示:
无论底层操作系统如何,都是如此,使用 32 位指针类型编译的应用程序只能指定 0 到 232-1 之间的地址。请注意,由于使用虚拟内存,应用程序中可见的地址可能不对应于特定的物理内存地址。
Assuming the documentation for
id()
is correct and CPython (x86) returns the address of the object, the maximum value that can be returned is 232-1 (4294967295). This can be demonstrated in a simple C program:This is true regardless of the underlying OS, an application compiled with a 32-bit pointer type can only specify addresses between 0 and 232-1. Note that the address visible in the application may not correspond to a particular physical memory address, due to the use of virtual memory.
你的猜测似乎有道理,但我不知道它们是否正确。
如果您需要依赖实现细节的行为(即文档中未指定),并且您需要确定,那么我想唯一要做的就是阅读源代码了解您正在使用的版本并了解它的功能。
Your guesses seem reasonable, but I have no idea if they're correct.
If you need to rely on behaviour that is an implementation detail (i.e. isn't specified in the docs), and you need to be sure, then I guess the only thing to do is read the source code for the version you're using and find out what it does.
Windows 将在其编译环境(32 或 64 位)中运行应用程序,而不是在其运行的 CPU 上运行。例如,运行 Windows 7 的 x64 计算机将在 32 位环境中运行 32 位 python。
因此,运行为 x86 编译的 python 将始终使用 32 位地址空间,这意味着 id() 将始终返回映射到唯一 32 位指针的值。 (请注意,该实现可能会做一些奇怪的事情,例如对指针加盐或使用 64 位或类似的一些疯狂的事情。)
Windows will run an application in the environment (32 or 64 bit) that it was compiled for, not the CPU it is running on. E.g., an x64 computer running Windows 7 will run 32-bit python in a 32-bit environment.
So, running python compiled for x86 will always use a 32-bit address space, which means that
id()
will always return a value that maps to a unique 32-bit pointer. (Note that the implementation may do some weird stuff like salting the pointer or using 64-bits or some crazy stuff like that.)