Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do objects work?
#1
How do objects work in python? For example:
x = 5
Is 5 the object which x refers to or is there an object in memory that contains the value 5?

Also when does an object get given a unique identifier? At compile time, upon creation or some other time? Please explain in detail or link me any good information, thanks!!
Reply
#2
There is an object in memory containing the value 5. Concretely it is a sequence of bytes in memory and the value 5 is written somewhere in binary form on these bytes. The object receives a unique ID when the memory for this object is allocated. Normally python uses the address in memory as the object's ID, which is an easy way to guarantee that different objects have different IDs. The only thing to know is that once the object has been deallocated, the same address, ID can be reused for another object. Live example:
>>> x = []
>>> id(x)
140240781096328
>>> x = []
>>> id(x)
140240781067400
>>> x = []
>>> id(x)
140240781096328
Reply
#3
An important point that comes from this - as long as an object has a reference pointing to it, it is live and kept. When there are no references it is available for reuse (garbage collection). The equals sign establishes a reference to the object. So:
x = [5,6]
print(x)
y = x
print(y)
x.append(7)
print(y)
x = [3]
print(x,y)
gives output of
Output:
[5, 6] [5, 6] [5, 6, 7] [3] [5, 6, 7]
The append in line 5 adds a 7 to the list referenced by x. It is the same list referenced by y (the line y=x does NOT make a copy, rather adds a second reference to the same object). So, printing y shows the 7 even though you did not modify y anywhere. When you reassign x to a different object in line 7, the first list remains referenced by y and the new list is referenced by x.
Reply
#4
Thanks for the replies, I’ve been reading alot of sources and from what I’ve read I don’t think python has any memory addresses rather it has unique identifiers which is the closest thing to a memory address. Also when the object has zero reference counts and it gets reused, will it’s unique identifier change(regardless if the value is the same as the previous one)?, thanks!
Reply
#5
(Oct-24-2019, 03:06 PM)pyThon_ Wrote: Thanks for the replies, I’ve been reading alot of sources and from what I’ve read I don’t think python has any memory addresses rather it has unique identifiers which is the closest thing to a memory address.
Python has and use memory address,just that is more flexible than in eg other languages.
>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)

>>> a = 5
>>> id(a)
1784207600
So 1784207600 is the memory address of object a.
Python has names or fancier identifiers that can point to same memory address.
[Image: python-gc-4-638.jpg?cb=1400908049]
Quote:Also when the object has zero reference counts and it gets reused, will it’s unique identifier change(regardless if the value is the same as the previous one)?, thanks!
Link see Reference counting.
Reply
#6
What makes you think the unique identifier is the memory address? The docs don’t say anything about it being an memory address, here this link will help out click me
Reply
#7
pyThon_ Wrote:What makes you think the unique identifier is the memory address?

This is CPython's definition of the id() function (this is C code)
static PyObject *
builtin_id(PyModuleDef *self, PyObject *v)
/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
{
    PyObject *id = PyLong_FromVoidPtr(v);

    if (id && PySys_Audit("builtins.id", "O", id) < 0) {
        Py_DECREF(id);
        return NULL;
    }

    return id;
}
whereas PyLong_FromVoidPtr()'s definition is
PyObject *
PyLong_FromVoidPtr(void *p)
{
#if SIZEOF_VOID_P <= SIZEOF_LONG
    return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p);
#else

#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
#   error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)"
#endif
    return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p);
#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */

}
It means that CPython casts the object's address to unsigned long or unsigned long long, then casts this value to a python int object to build the identifier.

That the object's ID is related to the memory address is simply not guaranteed by the specifications of the python language and it can be implemented differently.
Reply
#8
(Oct-24-2019, 03:54 PM)pyThon_ Wrote: What makes you think the unique identifier is the memory address? The docs don’t say anything about it being an memory address, here this link will help out click me
Sure if want to dig deeper and find where the address is in C using ctypes as you see in your link a couple of post further.

See what help(id) return.
>>> help(id)
Help on built-in function id in module builtins:

id(obj, /)
    Return the identity of an object.
    
    This is guaranteed to be unique among simultaneously existing objects.
    (CPython uses the object's memory address.)
Then have to explain that this is the Python implementation memory address.
To get the C level address has to use ctypes or what's the assembly code or better 000100100110001 adress ,just stop Hand
Dos this matter at all?
Reply
#9
(Oct-24-2019, 04:16 PM)Gribouillis Wrote: It means that CPython casts the object's address to unsigned long or unsigned long long, then casts this value to a python int object to build the identifier.

Thanks you for the info, so the unique identifier is derived from the memory address?
Reply
#10
Gribouillis Wrote:That the object's ID is related to the memory address is simply not guaranteed by the specifications of the python language and it can be implemented differently.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do integer objects work? pyThon_ 2 1,886 Oct-22-2019, 01:36 AM
Last Post: wavic
  Python - Make Json objects to work concurrently through Threads? WeInThis 0 2,611 Sep-22-2017, 11:31 AM
Last Post: WeInThis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020