What ought to persist after a program is run?

Discussion in 'Python' started by Thomas Philips, Jun 16, 2004.

  1. Here's a very simple program with an odd twist:
    class Player(object):
    def __init__(self,name):
    self.name = name

    hero = Player("A")
    print "hero",hero

    If I run it in IDLE and then type dir() at the prompt, I get
    >>>['Player', '__builtins__', '__doc__', '__name__', 'hero']


    However, if I modify the program as follows
    class Player(object):
    def __init__(self,name):
    self.name = name
    def main():
    hero = Player("A")
    print "hero=",hero
    main()

    and then run it in IDLE and type dir at the prompt, I get
    >>>['Player', '__builtins__', '__doc__', '__name__']


    Why does 'hero' not appear in the workspace directory when main() is
    invoked and vice versa?

    Thomas Philips
     
    Thomas Philips, Jun 16, 2004
    #1
    1. Advertising

  2. Thomas Philips

    Peter Hansen Guest

    Thomas Philips wrote:

    > Why does 'hero' not appear in the workspace directory when main() is
    > invoked and vice versa?


    Variables are considered local to functions unless explicitly
    specified otherwise. Since you don't say anything about hero,
    it is local and therefore does not persist after main() completes.

    What you might be looking for is the global keyword. If you
    put "global hero" anywhere in main() before you use the name
    hero, any references to it are treated as global to the module
    instead of local, and it will then appear in the "workspace
    directory"** after main() completes.

    -Peter

    ** By that term, I assume you mean it appears when you type dir()
    at an interactive prompt or something. The term has no meaning
    to me (perhaps because I don't use IDLE).
     
    Peter Hansen, Jun 16, 2004
    #2
    1. Advertising

  3. Thomas Philips

    Donn Cave Guest

    In article <>,
    (Thomas Philips) wrote:

    > Here's a very simple program with an odd twist:
    > class Player(object):
    > def __init__(self,name):
    > self.name = name
    >
    > hero = Player("A")
    > print "hero",hero
    >
    > If I run it in IDLE and then type dir() at the prompt, I get
    > >>>['Player', '__builtins__', '__doc__', '__name__', 'hero']

    >
    > However, if I modify the program as follows
    > class Player(object):
    > def __init__(self,name):
    > self.name = name
    > def main():
    > hero = Player("A")
    > print "hero=",hero
    > main()
    >
    > and then run it in IDLE and type dir at the prompt, I get
    > >>>['Player', '__builtins__', '__doc__', '__name__']

    >
    > Why does 'hero' not appear in the workspace directory when main() is
    > invoked and vice versa?


    Because main(), like any function, binds the object to its
    local namespace, not the "global" (module) namespace.

    Each function has its own, separate namespace, every time
    it's invoked. Ordinarily, the module namespace is mostly
    populated with functions, classes etc. defined in the
    module source (or typed in at the keyboard in your case.)
    Functions use their own namespaces for scratch space,
    and "return" the results of their computation, or modify
    input parameters. This isolation simplifies interdependencies
    between functions, so they're easier to manage as code gets
    rewritten.

    Donn Cave,
     
    Donn Cave, Jun 16, 2004
    #3
    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. John Latter
    Replies:
    4
    Views:
    414
    John Latter
    Sep 7, 2003
  2. John Nagle
    Replies:
    9
    Views:
    1,702
    Paul Boddie
    Jan 19, 2008
  3. Szabolcs Horvát
    Replies:
    25
    Views:
    745
    Rhamphoryncus
    May 9, 2008
  4. John Nagle
    Replies:
    43
    Views:
    1,143
    John Nagle
    Jul 7, 2009
  5. Ted Byers
    Replies:
    11
    Views:
    200
Loading...

Share This Page