Re: Executing global code

Discussion in 'Python' started by Peter Otten, Jan 15, 2009.

  1. Peter Otten

    Peter Otten Guest

    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
     
    Peter Otten, Jan 15, 2009
    #1
    1. Advertising

  2. Peter Otten

    Zac Burns Guest

    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 <> 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
    >
     
    Zac Burns, Jan 15, 2009
    #2
    1. Advertising

  3. Peter Otten

    Zac Burns Guest

    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 <> 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 <> 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
    >>

    >
     
    Zac Burns, Jan 15, 2009
    #3
  4. 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
     
    Steven D'Aprano, Jan 15, 2009
    #4
  5. Peter Otten

    MRAB Guest

    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.
     
    MRAB, Jan 15, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    792
    Alvin Bruney
    Jul 29, 2003
  2. Wayne
    Replies:
    2
    Views:
    476
    Wayne
    Nov 11, 2003
  3. Daniel Bass
    Replies:
    2
    Views:
    6,884
    Daniel Bass
    Dec 31, 2003
  4. =?Utf-8?B?QXNoYQ==?=

    executing client side code from code behind.

    =?Utf-8?B?QXNoYQ==?=, Aug 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    596
    =?Utf-8?B?QWJL?=
    Aug 12, 2004
  5. Jean-Daniel
    Replies:
    2
    Views:
    221
    Jean-Daniel
    Feb 12, 2011
Loading...

Share This Page