Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Executing global code

Reply
Thread Tools

Re: Executing global code

 
 
Peter Otten
Guest
Posts: n/a
 
      01-15-2009
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

 
Reply With Quote
 
 
 
 
Zac Burns
Guest
Posts: n/a
 
      01-15-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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
 
 
 
Zac Burns
Guest
Posts: n/a
 
      01-15-2009
The first line: doLoad = False, is to be ignored.
--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 15, 2009 at 10:30 AM, Zac Burns <(E-Mail Removed)> wrote:
> 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 <(E-Mail Removed)> 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
>>

>

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-15-2009
On Thu, 15 Jan 2009 17:50:54 +0100, Peter Otten 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


Unless you move the value of x into external storage. Untested:


try:
f = open('mystorage.txt', 'r')
except IOError:
x = 42
else:
x = int(f.read())
x += 1


Naturally the above is not bulletproof enough for production use.

I'm curious why the Original Poster wants to do such a thing, and
particularly the prohibition against moving code into a function.



--
Steven
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      01-15-2009
Steven D'Aprano wrote:
> On Thu, 15 Jan 2009 17:50:54 +0100, Peter Otten 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

>
> Unless you move the value of x into external storage. Untested:
>
>
> try:
> f = open('mystorage.txt', 'r')
> except IOError:
> x = 42
> else:
> x = int(f.read())
> x += 1
>
>
> Naturally the above is not bulletproof enough for production use.
>
> I'm curious why the Original Poster wants to do such a thing, and
> particularly the prohibition against moving code into a function.
>

Perhaps there are functions which contain 'global'. Anyway, the solution
has already been given, namely put it inside a loop, unless there's soem
reason why that's not possible.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
executing a function with feeding its global variables Jean-Daniel Python 2 02-12-2011 11:09 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
executing client side code from code behind. =?Utf-8?B?QXNoYQ==?= ASP .Net 1 08-12-2004 06:15 AM
Global Error handling in Applicatio_Error() of Global.asax VSK ASP .Net 1 07-29-2003 03:12 AM



Advertisments