2

When we look at the following code,

my_var = "Hello World"
id(my_var)

The statement id(my_var) returns the address/location of the string-object "Hello World"

I was wondering if we have any command, with which I can get the address/location of my_var


I am trying to understand memory in Python. For example, in C-programming I can get the address of variable and pointer in following way

int var;
int *var_ptr = &var;

printf ("%d", var_ptr);  % Prints address of var
printf ("%d", &var_ptr); % Prints address of var_ptr
13
  • 6
    You're thinking far too low level for a language like Python. id(my_var) returning the address of the String isn't even standard. That is, afaik, an implementation detail of CPython. Commented Jul 19, 2020 at 17:09
  • hmm.. may be.. I just started understanding how "Garbage collection" works in Python. So, I got this doubt... + I truly started appreciating C :) Commented Jul 19, 2020 at 17:12
  • Does this answer to your question : stackoverflow.com/questions/121396/… Commented Jul 19, 2020 at 17:12
  • Does this answer your question? Accessing Object Memory Address Commented Jul 19, 2020 at 17:12
  • You really don't ever need to worry, or even think about addresses while dealing with Python unless you're doing something niche like interfacing with C. Commented Jul 19, 2020 at 17:13

1 Answer 1

2

You can’t, but the reason is so fundamental that I think it worth posting anyway. In C, a pointer can be formed to any variable, including another pointer. (Another pointer variable, that is: you can write &p, but not &(p+1).) In Python, every variable is a pointer but every pointer is to an object.

A variable, not being an object, cannot be the referent of a pointer. Variables can however be parts of objects, accessed either as o.foo or o[bar] (where bar might be an index or a dictionary key). In fact, every variable is such an object component except a local variable; as a corollary, it is impossible to assign to a local variable from any other function. By contrast, C does that regularly by passing &local to whatever other function. (There is an exception to the "any other function": nonlocal variables can be assigned, but every local variable used in a nested function is implemented by implicitly creating a cell object as the underlying value of the variable and interpreting usage of the variable as referring to an attribute of it!)

This distinction is readily illustrated by C++ containers: they typically provide operator[] to return a reference (a pointer that, like a Python reference, is automatically dereferenced) to an element to which = can be applied, whereas the Python equivalent is to provide both __getitem__ (to return a reference) and __setitem__ (which implements []= all at once to store to a variable).

In CPython’s implementation, of course, each Python variable is a PyObject* variable, and a PyObject** to one can be used for internal purposes, but those are always temporary and do not even conceptually exist at the Python level. As such, there is no equivalent for id for them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.