Reference Tracking

Alex Martelli aleax at aleax.it
Tue Apr 15 04:30:08 EDT 2003


Ganesan R wrote:

> 
>>>>>>>> "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.

No!  the object that get_referrers is returning is the dictionary
of the module -- that dictionary refers to all variables (that's
where they live); it's NOT that variables refer to each other!


> Thanks for the clarifications.

You're welcome,

Alex





More information about the Python-list mailing list