Inverse of id()?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun May 20 14:28:14 EDT 2007


En Sat, 19 May 2007 20:42:53 -0300, Paul McGuire <ptmcg at austin.rr.com>  
escribió:

>>>> z = id(results)
>>>> for x in globals().values():
> ...   if id(x)==z: break
> ...
>
> This gives me a variable x that is indeed another ref to the results
> variable:
>>>> x is results
> True
>>>> x.x
> 123
>
> Now is there anything better than this search technique to get back a
> variable, given its id?

py> class A:pass
...
py> class B:pass
...
py> a=A()
py> id(a)
10781400
py> del a
py> b=B()
py> id(b)
10781400

Now if you look for id=10781400 you'll find b, which is another,  
absolutely unrelated, object.
Enabling this pointer -> objects ability would bring into Python the "wild  
pointer" nightmare of C programs...

-- 
Gabriel Genellina




More information about the Python-list mailing list