473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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 ....
Sep 22 '08 #1
7 5516
On Sep 22, 2:31*pm, CapnBearbo...@googlemail.com 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.

Matt
Sep 22 '08 #2
On Sep 22, 5:52*pm, Matimus <mccre...@gmail.comwrote:
On Sep 22, 2:31*pm, CapnBearbo...@googlemail.com 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.

Matt
ok. thanks! guess i'll be off to define my own function ...
Sep 22 '08 #3
Ca***********@googlemail.com wrote:
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

Sep 22 '08 #4
On Sep 22, 5:44*pm, Terry Reedy <tjre...@udel.eduwrote:
CapnBearbo...@googlemail.com wrote:
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

Sep 22 '08 #5
On Sep 22, 11:07*pm, CapnBearbo...@googlemail.com wrote:
On Sep 22, 5:52*pm, Matimus <mccre...@gmail.comwrote:
On Sep 22, 2:31*pm, CapnBearbo...@googlemail.com 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.
Matt

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]
Sep 23 '08 #6
MRAB wrote:
>
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]
--
http://mail.python.org/mailman/listinfo/python-list
Sep 23 '08 #7
Terry Reedy wrote:
MRAB wrote:
>>
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).
Sep 23 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jay | last post by:
I previously posted this question under Visual Basic newsgroup, but was advised to re-post here. I'm hoping someone can help me solve an issue I'm having with VB.Net and Access 2000. Here's...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
2
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two...
5
by: darrel | last post by:
I have a 'cancel' button that should reload the current page. Ie, reset the page back to it's initial state. Is the best way to do this to simply have the event handler for that button redirect...
5
by: Ellis Yu | last post by:
Dear All I want to read the short date format pattern from the regional setting. I write a code as follow: 'Diff. machine may has different country setting, so I use blank here Dim a as new...
3
by: Brian Hoops | last post by:
Is there an easy way to configure a site to load certain pages based on a short URL entered? For example... www.domain.com/sitemap loads www.domain.com/application/sitemap.aspx?show=all...
4
by: bejiz | last post by:
Hello, I have written a short program for practising linked lists. But there is surely something wrong for when I compile there is a unhandled exception and it does not print if I try to add more...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
6
by: Chris Nelson | last post by:
I want to create a "table" using CSS to display book covers and short reviews where the This is some test, yada, yada, yada +----------+ | | | | | img | | | |...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.