Executing global code

Zac Burns zac256 at gmail.com
Thu Jan 15 19:30:18 CET 2009


I'm not sure I fully understand the question "no moving the code to a
function", but you can prevent reload in a module by doing something
like this:

doLoad = False
try:
  no_reload
except NameError:
  no_reload = True
else:
  raise RuntimeError, "This module is not meant to be reloaded."

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 15, 2009 at 8:50 AM, Peter Otten <__peter__ at web.de> wrote:
> Jakub Debski wrote:
>
>> Is it possible to execute global code (module-level code) more than
>> once keeping the state of global variables? This means no reload() and
>> no moving the code to a function.
>
> You have a module containing e. g. these two statements
>
> x = 42
> x += 1
>
> and want to rerun it with the effect of x becoming 44? That is not possible
> because in Python
>
> x = 42
>
> is a statement, too, and will thus be rerun.
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list