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
  • 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. – Carcigenicate Jul 19 '20 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 :) – Aravind D. Chakravarti Jul 19 '20 at 17:12
  • Does this answer to your question : https://stackoverflow.com/questions/121396/accessing-object-memory-address – jossefaz Jul 19 '20 at 17:12
  • Does this answer your question? [Accessing Object Memory Address](https://stackoverflow.com/questions/121396/accessing-object-memory-address) – jossefaz Jul 19 '20 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. – Carcigenicate Jul 19 '20 at 17:13
  • C includes memory access as part of its data model. Python does not. As far as the *language* is concerned, every single variable lookup could be backed by an HTTP GET call to some remote server. (Slow, yes, but it wouldn't change the semantics of the language in any way.) – chepner Jul 19 '20 at 17:19
  • Even as an implementation detail, nothing prevents Python from moving an object to a different location in memory during garbage collection, as long as its ID remains unique. – chepner Jul 19 '20 at 17:21
  • @yossefaz : No, the response provided in that answer, actually returns the address of the "Hello World" (I just ran code, and re-checked) – Aravind D. Chakravarti Jul 19 '20 at 17:23
  • @Carcigenicate Thanks for your response.. May be it is not possible in Python to go that "deep" – Aravind D. Chakravarti Jul 19 '20 at 17:25
  • @chepner: Hmm.. Okay.. That may be possible. – Aravind D. Chakravarti Jul 19 '20 at 17:26
  • @AravindD.Chakravarti I'm curious, why are you wanting to "go that deep"? Is there a particular reason, or just interest? – Carcigenicate Jul 19 '20 at 17:27
  • A Python variable doesn't even *have* an address of its own. Whatever value it holds at any given moment in time will have an address, but that will usually change when a new value is assigned to the variable. – jasonharper Jul 19 '20 at 17:27
  • @Carcigenicate : Just interest :-) I understand this is not required during development of any code :-) – Aravind D. Chakravarti Jul 19 '20 at 17:29

1 Answers1

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.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76