[Edu-sig] Reference Count

Michael H. Goldwasser goldwamh at slu.edu
Sat May 17 03:19:53 CEST 2008


On Friday May 16, 2008, kirby urner wrote: 

>    Trying to think why the refcount jumps to 5 when I check it inside g:
>    
>    >>> h = f
>    >>> sys.getrefcount( f )
>    3
>    
>    >>> def g(thefunc):
>    	thefunc.func_doc = "hey, different!"
>    	print sys.getrefcount(thefunc)
>    	
>    >>> g(h)
>    5
>    >>> sys.getrefcount(h)
>    3
>    
>    Clues anyone?  I'm not much of an under-the-hood guy.
>    
>    Kirby


We've gotten quite off track of the original thread, but to answer
your question, one of the extra counts is because of the local
variable 'thefunc' within the scope of g.  The other is due to the
formal parameter from within the scope of getrefcount. That is also
why it was originally 3 rather than 2 when you had h and f referencing
the object. See help(sys.getrefcount) for documentation.

>>> a = []
>>> sys.getrefcount(a)
2



       +-----------------------------------------------
       | Michael Goldwasser
       | Associate Professor
       | Dept. Mathematics and Computer Science
       | Saint Louis University
       | 220 North Grand Blvd.
       | St. Louis, MO 63103-2007



More information about the Edu-sig mailing list