1

I am looking for resources on how python uses the address representation found when printing an instance object <main.class object at 0x107857490>.

My guess is that this can't be a hardware address as I know that even C does not represent hardware address in code and instead simply gives you pointers that represent addresses in the C runtime.

Are there resources on implementation details in python like this?

Grayden Hormes
  • 855
  • 1
  • 15
  • 34
  • 1
    Assuming CPython, this is almost certainly just the value of the C pointer to the Python object. But, I'll see if I can find out for sure. What are you trying to do with this information? – Quelklef Jun 20 '20 at 01:23

1 Answers1

2

I'll assume you're asking about the CPython implementation in particular.

In the CPython Github repo, the file Objects/object.c seems to have our answer. On line 390 is defined the function PyObject_Repr(PyObject *v), which, after some checks, has the following code:

    if (Py_TYPE(v)->tp_repr == NULL)
        return PyUnicode_FromFormat("<%s object at %p>",
                                    Py_TYPE(v)->tp_name, v);

which reads to me as follows:

    // If the Python object v does not have a custom __repr__
    if (Py_TYPE(v)->tp_repr == NULL)

        // then return '<' + name_of_type + ' object at ' + v + '>'
        return PyUnicode_FromFormat("<%s object at %p>",
                                    Py_TYPE(v)->tp_name, v);

Assuming that PyUnicode_FromFormat uses the same syntax as printf, then %p gives the value of the pointer v.

Quelklef
  • 1,999
  • 2
  • 22
  • 37