Free Memory

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon May 12 00:39:14 EDT 2008


En Sun, 11 May 2008 01:06:13 -0300, Patrick Mullen <saluk64007 at gmail.com> escribió:

> Yeah I don't know much about locals or globals.  I've never used them
> before, just know they are there.  But anyway, to illustrate what I meant by
> the interesting behavior, here is the output (and sorry for it being so
> long):
>
> IDLE 2.6a2
>>>> globals()
> {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__',
> '__doc__': None, '__package__': None}
>>>> globals().clear()
>>>> globals()
> {'__builtins__': {'bytearray': <type 'bytearray'>, 'IndexError': <type
     [... long __builtins__ dictionary contents ...]
> 'exceptions.WindowsError'>}}
>>>>
>
> The OP wanted to clear the interpreter and I took a very broad (and highly
> unresearched) stab at it, and saw some behavior that I wasn't expecting.
> So before, all the builtin variables are hidden away in __builtins__, but
> after clearing globals, they are filled into globals.  This doesn't really
> make sense to me, but I don't know much about __builtins__ or globals.

Look more carefully:

py> globals().clear()
py> len(globals())
1
py> globals().keys()
['__builtins__']

globals() contains now a single entry, the namespace of the __builtin__ module (that is, __builtin__.__dict__), not the individual builtin entries (these are the dictionary entries that you posted).
As you later noticed, this happens only on IDLE. I don't know why IDLE behaves that way, but avoid entering "restricted mode" may be a reason (see http://docs.python.org/lib/restricted.html )
Usually, __builtins__ contains a reference to the __builtin__ module itself *only* in __main__; in all other modules, __builtins__ contains a reference to the __builtin__ namespace (like what you got in IDLE after clearing globals). I don't know *why* they are different either.

-- 
Gabriel Genellina




More information about the Python-list mailing list