I have the following function defined in my Spyder startup file:
# startup.py
#-----------
def del_globals(*names):
for name1 in names:
try: del globals()[name1]
except KeyError: pass # Ignore if name1 doesn't exist
At the Spyder console, I run the following script with the debugger to create a variable cat
before descending into a function:
# MyScript.py
#------------
runfile('The/Path/to/startup.py') # In case functions are deleted
cat='dog'
del_globals('cat')
I have a breakpoint at the last line del_globals('cat')
. Upon breaking, I confirm that cat
is a variable in the global namespace:
sorted(globals().keys())
# ['TKraiseCFG', '__builtins__', '__doc__', '__file__',
# '__loader__', '__name__', '__nonzero__', '__package__',
# '__spec__', 'cat', 'del_globals']
I then step into del_globals('cat')
and at the very first line (the for
statement), confirmed that cat
is not in the global namespace
sorted(globals().keys())
['TKraiseCFG', '__builtins__', '__doc__', '__loader__',
'__name__', '__nonzero__', '__package__', '__spec__',
'_spyderpdb_builtins_locals', '_spyderpdb_code',
'_spyderpdb_locals', 'del_globals']
Should there be just one global namespace, and should that be the prevailing namespace at the REPL command line?
P.S. For background, the context is described here and here.