Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

Unnamed Python objects have the same id

Let's create two lists:

x = range(3)
y = range(3)
print id(x), id(y)

Out:

4366592912 4366591040

I created two independent lists, and the output shows two different memory addresses. This is not surprising. But now let's do the same thing without the assignment:

id(range(3))

Out:

4366623376

And a second time:

id(range(3))

Out:

4366623376

I am not sure how to interpret this. Why do these two unnamed lists have the same memory address?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the doc of id(object):

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

Since the two ranges inside the id() calls have non-overlapping lifetimes, their id values may be the same.

The two ranges assigned to variables have overlapping lifetimes so they must have different id values.

Edit:

A look into the C sources shows us builtin_id:

builtin_id(PyObject *self, PyObject *v)
{
    return PyLong_FromVoidPtr(v);
}

and for PyLong_FromVoidPtr.

PyLong_FromVoidPtr(void *p)
{
#if SIZEOF_VOID_P <= SIZEOF_LONG
    return PyLong_FromUnsignedLong((unsigned long)(Py_uintptr_t)p);
#else

#ifndef HAVE_LONG_LONG
#   error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
#endif
#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
#   error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
#endif
    return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */

}

So the ID is a memory address.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...