Reference Tracking

Ganesan R rganesan at myrealbox.com
Tue Apr 15 04:23:52 EDT 2003


>>>>>>> "Alex" == Alex Martelli <aleax at aleax.it> writes:
>> 
>> Why am I getting a different list for the last call?

> Because you're asking about a distinct (even though equal) value,
> AKA object.  Python MAY but doesn't HAVE TO reuse the same object
> when you use several literals indicating equal immutables -- in
> practice, currently, it does for small integers, but (in Python
> 2.2) not for such strings as you used here.  Python can always
> change this in future releases, since this is strictly an issue of
> implementation -- optimizing one way or another, no more than that.

<andlx-anamika 1> ~ $ python
Python 2.2.1 (#1, Jul 29 2002, 23:15:49) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gc
>>> a = 'hello world'
>>> b = 'hello world'
>>> c = a 
>>> gc.get_referrers(b)
[{'a': 'hello world', 'c': 'hello world', 'b': 'hello world', 'gc': <module 'gc' (built-in)>, '__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None}]

The reason I was confused was because of the output from gc.get_referrers(b). 
I assumed that a and b held a reference to the same object. I should've
checked the ids of a and b. 

I now figured out that a is holding a reference to b because of some
python implementation detail. In fact all module variables seem to hold a
reference to other variables.  

Thanks for the clarifications. 

Ganesan

-- 
Ganesan R





More information about the Python-list mailing list