Newbie - Variable Scope

Discussion in 'Python' started by RN, Nov 17, 2003.

  1. RN

    RN Guest

    I have a 2 Python scripts that contain the following lines:

    test.py
    -------
    from testmod import *
    a1 = 10
    modfunc()

    testmod.py
    -----------
    def modfunc():
    print a1

    When I run test.py, it returns the following error:

    File "testmod.py", line 2, in modfunc
    print a1
    NameError: global name 'a1' is not defined

    My intent is to make a1 a global variable - so that I can access its value
    in all functions of imported modules. What should I do?

    Thanks in advance,

    Rohit
     
    RN, Nov 17, 2003
    #1
    1. Advertising

  2. RN

    KefX Guest

    >I have a 2 Python scripts that contain the following lines:
    >
    >test.py
    >-------
    >from testmod import *
    >a1 = 10
    >modfunc()
    >
    >testmod.py
    >-----------
    >def modfunc():
    > print a1
    >
    >When I run test.py, it returns the following error:
    >
    > File "testmod.py", line 2, in modfunc
    > print a1
    >NameError: global name 'a1' is not defined
    >
    >My intent is to make a1 a global variable - so that I can access its value
    >in all functions of imported modules. What should I do?
    >
    >Thanks in advance,
    >
    >Rohit


    I think this is technically one script (not "two scripts"), just in two
    modules, but whatever.

    First off, you're using the form "from x import y", (in this case, x is
    'testmod' and y is '*') which shouldn't be used for your own modules unless you
    know what you're doing. It is better to write this as "import x".

    This has nothing to do with the problem, though. What you have to do for your
    specific test case is have testmod.py import test.py, because it uses a
    variable that's defined in test.py. Using "from x import *" doesn't make the x
    act like a C-style include file, though there are many similarities.

    Of course, it's best to avoid situations where two modules import each other
    altogether. I would put the globals in a third module and have everything
    import that module. Thus, one solution would be this:

    test.py
    -------
    import testmod
    import globals

    globals.a1 = 10
    testmod.modfunc()


    testmod.py
    ----------
    import globals

    def modfunc():
    print globals.a1


    globals.py
    ----------
    # Blank, but you can define 'a1' here instead of in test.py if it's more
    appropriate


    Of course, the uber-best solution is to avoid globals altogether, but that's
    another thing...

    - Kef
     
    KefX, Nov 17, 2003
    #2
    1. Advertising

  3. On Mon, 17 Nov 2003 16:02:14 +0530, "RN"
    <> wrote:

    >I have a 2 Python scripts that contain the following lines:
    >
    >test.py
    >-------
    >from testmod import *
    >a1 = 10
    >modfunc()
    >
    >testmod.py
    >-----------
    >def modfunc():
    > print a1
    >
    >When I run test.py, it returns the following error:
    >
    > File "testmod.py", line 2, in modfunc
    > print a1
    >NameError: global name 'a1' is not defined
    >
    >My intent is to make a1 a global variable - so that I can access its value
    >in all functions of imported modules. What should I do?
    >


    The only truly global names in Python are the builtin ones and you
    shouldn't be mucking with those unless you have a very good reason to.
    Plain global variables are just global at module-level not across
    modules.

    To solve your problem:

    - import test.py everywhere you need a1.

    - Even better, reorganize your code so that you do not need global
    variables in the first place.

    Any more questions just holler, with my best regards,
    G. Rodrigues
     
    Gonçalo Rodrigues, Nov 17, 2003
    #3
  4. RN

    Duncan Booth Guest

    "RN" <> wrote in
    news::

    > test.py
    > -------
    > from testmod import *


    Don't use 'from testmod import *'. It will just cause confusion. Use
    'import testmod' instead.

    > a1 = 10


    Use: testmod.a1 = 10

    You want to set a1 in the other module. If you had a global variable called
    a1 in the other module, assigning to a1 in this module would have no
    effect. They are two different variables. And that is why you don't want to
    use the 'from' variant of import, it simply assigns the value of each
    variable from testmod into an unrelated variable of the same name in the
    local module when you want to maintain a relationship between them.

    > modfunc()

    Use: testmod.modfunc()

    >
    > testmod.py
    > -----------
    > def modfunc():
    > print a1


    If you are going to have a variable called 'a1' in this module, it would be
    nicer to initialise it here so as to make it obvious. So add:

    a1 = 0

    outside the function.
    >
    > When I run test.py, it returns the following error:
    >
    > File "testmod.py", line 2, in modfunc
    > print a1
    > NameError: global name 'a1' is not defined
    >
    > My intent is to make a1 a global variable - so that I can access its
    > value in all functions of imported modules. What should I do?


    By the way, a1 isn't a terribly good name for a global variable; it doesn't
    convey anything at all about the purpose of the variable. If you must use
    global variables (and you should think twice or thrice before you do), you
    should at least give them purposeful names.

    --
    Duncan Booth
    int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
    "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
     
    Duncan Booth, Nov 17, 2003
    #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. biner
    Replies:
    1
    Views:
    365
    Joona I Palaste
    Aug 30, 2004
  2. Colin Walls
    Replies:
    3
    Views:
    1,090
    Colin Walls
    Oct 10, 2004
  3. Steve Cooper
    Replies:
    2
    Views:
    141
    Kenta MURATA
    Jan 18, 2004
  4. David Filmer
    Replies:
    19
    Views:
    282
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    219
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page