Reference Tracking
Greg Ewing (using news.cis.dfn.de)
ckea25d02 at sneakemail.com
Mon Apr 14 23:33:51 EDT 2003
-- any number of system-maintained
> data structures may have attributes and items that happen to
> be worth 1 (or 0, or other small, popular numbers).
>
> import gc
> gc.get_referrers(1)
>
> will at any time give you a list of all containers which have
> a reference to the value 1, if you're curious. Of course, having
> such a container in hand will not necessarily make it easy to
> understand WHAT the container in fact IS -- but many will be
> dicts with string keys, which lets you do some detective work;-).
>
>
> Note that any Python function call always passes VALUES -- i.e.
> there is no difference between your call to sys.getrefcount in
> the above snippet and one that directly does sys.getrefcount(1).
> In either case, it's the VALUE (1) that you're asking info about;
> it makes no difference HOW you obtain the value to pass (I guess
> this is probably already clear to you, but, just in case...).
>
>
> In your case, you were talking about tracking objects of a specific
> different type -- not ones that Python will implicitly cache and
> reuse, such as small integers. Thus, you do not necessarily need
> to work with _changes_ in refcounts rather than with refcounts
> directly -- I think using the refcounts directly may be simpler.
>
>
> Alex
>
Alex Martelli wrote:
> In practice, Python does such caching for small integers, and 1
> is clearly a popular value
Just for fun, I did a quick poll of the popularities
of integers from 0-99:
>>> for i in range(100): print i, sys.getrefcount(i)
...
0 98
1 45
2 21
3 9
4 14
5 10
6 11
7 10
8 10
9 9
10 14
11 8
12 8
13 10
14 7
15 7
16 6
17 9
18 7
19 7
20 7
21 7
22 7
23 8
24 7
25 7
26 7
27 7
28 7
29 9
30 8
31 7
32 9
33 5
34 5
35 5
....
99 5
It seems that the most popular number is 0, and the most
popular popularity is 5.
0-for-prime-minister-ly,
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list