On Fri, May 23, 2014, at 8:49, Chandra Srinivasan wrote:
> Hi,
> I ran the following code in the Python interpreter and am trying to
> determine if the behavior I see is expected:
> 
> import sys
> print sys.getrefcount(globals())
> class Foo(object):
>    def __init__(self):
>       pass
> print sys.getrefcount(globals())
> 
> The first print statement above prints '4' and the second one prints '5'.
> However, if I remove the __init__ method from the class, the refcount
> stays the same.
> 
> If I change the above code like this, the ref count stays the same:
> 
> import gc
> import sys
> print sys.getrefcount(globals())
> class Foo(object):
>    def __init__(self):
>       pass
> del Foo
> while gc.collect():
>   pass
> print sys.getrefcount(globals())
> 
> Can you let me know if this is a bug in the Python interpreter?

No, functions hold a reference to the globals of the module they are
defined in.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to