a short-cut command for globals().clear() ??

C

CapnBearbossa

hi all,

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question. I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace. In other words a statement, or
some_command_or_function(), that does this:
['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
['__builtins__', '__doc__', '__name__']

thanks,
1 desperate snake oil programmer ....
 
M

Matimus

hi all,

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:

['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']

['__builtins__', '__doc__', '__name__']

thanks,
   1 desperate snake oil programmer ....

I don't think you will find anything. The interpreter is essentially
the same whether you are in interactive mode or not. That is, there is
very little use for a method that clears globals in general, so why
would we add it just so that it could be used by the interpreter.
There is almost* nothing available to the interactive interpreter
which isn't part of the core language.

* The only difference I can think of is the "_" variable, which is
added to __builtins__ and contains the last value returned in
interactive mode. If you have ever tried to run code that uses the
locale module from the interpreter you will see why having any
differences between the interactive and non-interactive interpreter
can be a pain.

Matt
 
C

CapnBearbossa

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:

['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
['__builtins__', '__doc__', '__name__']
thanks,
   1 desperate snake oil programmer ....

I don't think you will find anything. The interpreter is essentially
the same whether you are in interactive mode or not. That is, there is
very little use for a method that clears globals in general, so why
would we add it just so that it could be used by the interpreter.
There is almost* nothing available to the interactive interpreter
which isn't part of the core language.

* The only difference I can think of is the "_" variable, which is
added to __builtins__ and contains the last value returned in
interactive mode. If you have ever tried to run code that uses the
locale module from the interpreter you will see why having any
differences between the interactive and non-interactive interpreter
can be a pain.

Matt

ok. thanks! guess i'll be off to define my own function ...
 
T

Terry Reedy

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question. I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace. In other words a statement, or
some_command_or_function(), that does this:
['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
['__builtins__', '__doc__', '__name__']

First, a WARNING to other readers deceived by the subject line.
Globals().clear() clears everything and leaves nothing, so Capn... is
looking for something that works that is a shortcut for deleting
bindings one-by-one.

To your question. The short answer is no.

In batch mode, only create what you need and delete (unbind) large
objects that are not automatically deleted (unbound) when you are done
with them. Remember that only reference-counted implementations will
guarantee immediate destruction and space-freeing when the last
reference goes away. Check the gc module (and some posts in the
archives) for more specialized control.

In interactive mode, restart the interpreter if you really need a clean
slate and have too many bindings that you must delete to do something
quick like 'del x,y,z' as in your example above. In IDLE, cntl-F6
restarts the shell with a clean slate. I presume IPython has something
similar.

tjr
 
A

Aaron \Castironpi\ Brady

forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:
x=3
y=4
z=[]
dir()
['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
some_command_or_function()
dir()
['__builtins__', '__doc__', '__name__']

First, a WARNING to other readers deceived by the subject line.
Globals().clear() clears everything and leaves nothing, so Capn... is
looking for something that works that is a shortcut for deleting
bindings one-by-one.

To your question.  The short answer is no.

In batch mode, only create what you need and delete (unbind) large
objects that are not automatically deleted (unbound) when you are done
with them.  Remember that only reference-counted implementations will
guarantee immediate destruction and space-freeing when the last
reference goes away. Check the gc module (and some posts in the
archives) for more specialized control.

In interactive mode, restart the interpreter if you really need a clean
slate and have too many bindings that you must delete to do something
quick like 'del x,y,z' as in your example above.  In IDLE, cntl-F6
restarts the shell with a clean slate.  I presume IPython has something
similar.

tjr

I guess you have a few hackish options.

1) Define a wrapper that calls its argument immediately, and use it as
you would use anonymous blocks.

@call_imm
def block( ):
code
code
code

@call_imm
def block( ):
code
code
code

You have no globals when you are done.

2) If you need them to be global, use some introspection, and delete
the variables upon exiting the scope.

3) Declare all your variables in a namespace, and just delete the
namespace.

a= type('blank',(),{})()
a.varA= 0
a.varB= 'abc'
del a
 
M

MRAB

On Sep 22, 2:31 pm, (e-mail address removed) wrote:
hi all,
forgive me , but the RTFM and Google search approaches are not
yielding an answer on this question.  I need to know if there's a top
level python interpreter command that clears all user variables (not
built-ins) from the global namespace.  In other words a statement, or
some_command_or_function(), that does this:
x=3
y=4
z=[]
dir()
['__builtins__', '__doc__', '__name__', 'x', 'y', 'z']
some_command_or_function()
dir()
['__builtins__', '__doc__', '__name__']
thanks,
   1 desperate snake oil programmer ....
I don't think you will find anything. The interpreter is essentially
the same whether you are in interactive mode or not. That is, there is
very little use for a method that clears globals in general, so why
would we add it just so that it could be used by the interpreter.
There is almost* nothing available to the interactive interpreter
which isn't part of the core language.
* The only difference I can think of is the "_" variable, which is
added to __builtins__ and contains the last value returned in
interactive mode. If you have ever tried to run code that uses the
locale module from the interpreter you will see why having any
differences between the interactive and non-interactive interpreter
can be a pain.

ok. thanks! guess i'll be off to define my own function ...

How about something like this:

def clear_workspace():
keep_set = set(['__builtins__', '__doc__', '__name__',
'clear_workspace'])
for x in globals().keys():
if x not in keep_set:
del globals()[x]
 
T

Terry Reedy

MRAB said:
How about something like this:

def clear_workspace():
keep_set = set(['__builtins__', '__doc__', '__name__',
'clear_workspace'])

For 2.6/3.0, add __package__ to the list to be kept.
for x in globals().keys():
if x not in keep_set:
del globals()[x]
 
R

Ricardo Aráoz

Terry said:
MRAB said:
How about something like this:

def clear_workspace():
keep_set = set(['__builtins__', '__doc__', '__name__',
'clear_workspace'])

For 2.6/3.0, add __package__ to the list to be kept.
for x in globals().keys():
if x not in keep_set:
del globals()[x]


Or... you might have a script clearWorkspace.py :

---------------------------------------------
initGlobals = globals().keys()

def clearWorkspace() :
for gVar in globals().keys() :
if gVar not in initGlobals :
del globals()[gVar]
---------------------------------------------

Which you run before doing anything else. Then you don't mind if
additions were made to the list to keep, or if you are using something
like pyCrust which has its own initial globals, or if you want to keep
some global vars of your own (in which case you run clearWorkspace AFTER
you instantiate your global vars).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top