what is happening here?

Discussion in 'Python' started by dan miller (moderator, s.p.d), Apr 6, 2004.

  1. trying to figure out scoping of 'globals' in modules. So I have test.py:

    glob = 1

    def setglob(v):
    global glob
    glob = v

    def getglob():
    return glob


    and I do this:

    >>> from test import *
    >>> glob

    1
    >>> getglob()

    1
    >>> glob=2
    >>> glob

    2
    >>> getglob()

    1
    >>> setglob(3)
    >>> getglob()

    3
    >>> glob

    2


    Seems to me like the first time I invoke glob, it creates a new global
    in my namespace, and initializes it to the value of the glob in test.py.
    Seems counterintuitive!
     
    dan miller (moderator, s.p.d), Apr 6, 2004
    #1
    1. Advertising

  2. dan miller (moderator, s.p.d)

    John Roth Guest

    "dan miller (moderator, s.p.d)" <> wrote in message
    news:c4vaum$q39$...
    > trying to figure out scoping of 'globals' in modules. So I have test.py:
    >
    > glob = 1
    >
    > def setglob(v):
    > global glob
    > glob = v
    >
    > def getglob():
    > return glob
    >
    >
    > and I do this:
    >
    > >>> from test import *
    > >>> glob

    > 1
    > >>> getglob()

    > 1
    > >>> glob=2
    > >>> glob

    > 2
    > >>> getglob()

    > 1
    > >>> setglob(3)
    > >>> getglob()

    > 3
    > >>> glob

    > 2
    >
    >
    > Seems to me like the first time I invoke glob, it creates a new global
    > in my namespace, and initializes it to the value of the glob in test.py.
    > Seems counterintuitive!


    Sure seems wierd. What it looks like to me is that
    your import is binding three objects, but the actual
    functions are still refering to whatever object is bound
    in the "test" module, not in the interpreter's namespace.
    In other words, they are using the global dictionary
    they were compiled with, not their caller's global
    dictionary.

    Make sense?

    John Roth
    >
     
    John Roth, Apr 6, 2004
    #2
    1. Advertising

  3. dan miller (moderator, s.p.d) wrote:
    > >>> from test import *


    Here you create a variable glob that is a reference to the one in your test
    module.

    > >>> glob

    > 1


    This returns the value of glob in the test module

    > >>> getglob()

    > 1


    Here you're calling getglob, which is in the test module.

    > >>> glob=2


    Here's where your problem lies... You're creating a new variable glob that
    *replaces* the old variable. The variable that is also called glob in the
    test module is unaffected.

    > >>> glob

    > 2


    Now you're refering to the newly create variable, hence the value 2.

    > >>> getglob()

    > 1


    Here you're calling getglob, which is in the test module and hence it uses
    the glob variable in test - which has the value 1.

    > >>> setglob(3)
    > >>> getglob()

    > 3


    Same with these two, they're both in test - and so use the variable glob in
    test.

    > >>> glob

    > 2


    This still refers to glob in your local namespace, which still has the value
    2.

    Hope this is of some help,
    Andrew
     
    Andrew Wilkinson, Apr 7, 2004
    #3
  4. dan miller (moderator, s.p.d)

    DomF Guest

    "dan miller (moderator, s.p.d)" <> wrote in message
    news:c4vaum$q39$...
    > trying to figure out scoping of 'globals' in modules. So I have test.py:
    >
    > glob = 1
    >
    > def setglob(v):
    > global glob
    > glob = v
    >
    > def getglob():
    > return glob
    >
    >
    > and I do this:
    >
    > >>> from test import *
    > >>> glob

    > 1
    > >>> getglob()

    > 1
    > >>> glob=2
    > >>> glob

    > 2
    > >>> getglob()

    > 1
    > >>> setglob(3)
    > >>> getglob()

    > 3
    > >>> glob

    > 2
    >
    >
    > Seems to me like the first time I invoke glob, it creates a new global
    > in my namespace, and initializes it to the value of the glob in test.py.
    > Seems counterintuitive!


    This is an instance of "from x import *" being harmful. Try this with just
    "import test" in the shell and it doesn't look confusing at all:

    >>> import test
    >>> glob

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'glob' is not defined
    >>> test.glob

    1
    >>> test.getglob()

    1
    >>> glob=2
    >>> glob

    2
    >>> test.getglob()

    1
    >>> test.setglob(3)
    >>> test.getglob()

    3
    >>> glob

    2

    Dom
     
    DomF, Apr 8, 2004
    #4
    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. Replies:
    3
    Views:
    411
    Howard
    Feb 22, 2005
  2. whats happening here

    , Mar 25, 2008, in forum: C Programming
    Replies:
    4
    Views:
    2,589
    Eric Sosman
    Mar 25, 2008
  3. vikramtheone

    Basics of VHDL. Whats happening here?

    vikramtheone, Jun 1, 2009, in forum: VHDL
    Replies:
    1
    Views:
    498
    jeppe
    Jun 1, 2009
  4. tschmittldk
    Replies:
    8
    Views:
    380
    Jorgen Grahn
    Dec 29, 2010
  5. George George

    what is happening here

    George George, Apr 8, 2009, in forum: Ruby
    Replies:
    6
    Views:
    136
    George George
    Apr 9, 2009
Loading...

Share This Page