11

Possible Duplicate:
Get object by id()?

>>> var = 'I need to be accessed by id!'
>>> address = id(var)
>>> print(address)
33003240

Is there any way to use address of var in memory, provided by id(), for accessing value of var?

UPD:
I also want to say, that if this cannot be done in standard Python, it also would be interesting, if this somehow could be implemented via hacking C++ internals of Python.

UPD2: Also would be interesting to know, how to change value of var.

Community
  • 1
  • 1
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
  • 5
    You can't. The return value of `id()` is not a reference. The fact that cpython returns a memory address here is a implementation detail, and using this as a reference is *not* the goal of the function. – Martijn Pieters Jan 10 '13 at 11:54
  • 1
    Why on any deity's name would you want to do that? Martijn is correct, even if you could somehow hack it in CPython, it's just an implementation detail. – pcalcao Jan 10 '13 at 11:58
  • Why do you want to do this? – Keith Jan 10 '13 at 11:59
  • What are you trying to achieve? There are probably other ways to achieve whatever you are need done. – Martijn Pieters Jan 10 '13 at 12:00
  • 2
    First goal: fun; Second: passing-by-reference - sometimes its needed for more flexibility, despite its not pythonic. Its a zen-question, no practical background. – Gill Bates Jan 10 '13 at 12:08
  • It also doesn't make sense that to get the `id` of an object, you've had to have a reference to the name to do so... So at that point, just keep the name hanging around rather than some convoluted in-direct method of getting it back... – Jon Clements Jan 10 '13 at 12:09
  • If it's only for fun you can create an id to object map: `{id(v):v for k,v in globals().iteritems()}` – root Jan 10 '13 at 12:11
  • @MartijnPieters I wanted to answer you but forgot then. Memory address is memory address - whatever programming language we use - memory address always remains what it is - address where chunk of data stored. Variable in python also have its address, but `id()` returns address of data to what points Python variable. In Python all variables are just pointers behind the scenes. – Gill Bates Jan 26 '13 at 19:56
  • It's possible. See accepted answer to duplicate question [**_Is it possible to dereference variable id's?_**](http://stackoverflow.com/questions/15011674/is-it-possible-to-dereference-variable-ids) – martineau Mar 11 '17 at 18:42

1 Answers1

12

A solution, only for your case, would be:

var = 'I need to be accessed by id!'
address = id(var)
print(address)
var2 = [x for x in globals().values() if id(x)==address]

It also works from inside a function like

def get_by_address(address):
    return [x for x in globals().values() if id(x)==address]

var = 'I need to be accessed by id!'
address = id(var)
print(address)
var2 = get_by_address(address)

But as others pointed out: First make sure there is no better solution that better fits your needs

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • 2
    See also http://stackoverflow.com/a/1396690/514485. And like almost everyone else I caution you, you probably don't want/need to do this. – jimhark Jan 10 '13 at 12:13