deleting global namespaces

martindvorak at my-deja.com martindvorak at my-deja.com
Sat Feb 5 11:15:21 EST 2000


> There is no guaranteed automatic way to make Python delete something.
The
> only way to 'delete' something is to remove all references to it, and
make
> the refcounter (or garbage collector, in Java) delete the object. But
it
> sounds to me like you are trying to use the wrong tool for the wrong
task --
> modules only get executed once, upon the first load time. The top
level of a
> module is intended to run just once in it's lifetime, not every time
you
> wish to reset the data to the starting point.
>
> To do what you want, either work with functioncalls to reset the
module to
> its initial state, or just use objects. Using objects is probably the
best
> method, as it groups data, methods and state in one easy-to-use
package ;)
>
> So you can do either:
>
> module.py:
> -----
> var = SomeState
> def reset():
> 	global var
> 	var = SomeState
>
> def do_something_with_var():
> 	...
> -----
>
> and manually call reset() when you wish to reset the data, or:
>
> module.py:
> -----
> class Module:
> 	def __init__(self):
> 		self.var = SomeState
> 	def do_something_with_var(self):
> 		...
> -----
>
> If you then wish to reset to the initial state, just create a new
> module.Module object.

That's clear but it does not solve my problem. I know
objects and use them extensively, but I need to divide
the whole application (which consists of hundrends
of objects) into independent components each with its own
global namespace so that component's objects can
use this global namespace independently on other
objects in other components.

Martin


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list