global variable not assigned, newbie question

Discussion in 'Python' started by Jean-Daniel Gamache, Nov 1, 2004.

  1. Hi! here's a python newbie question ...
    why is my variable (myNum) not assigned the value 10 ???

    ========

    myNum = 0

    def setNum():
    myNum = 10
    print myNum

    def main():
    setNum()
    print myNum

    if __name__ == '__main__':
    main()
     
    Jean-Daniel Gamache, Nov 1, 2004
    #1
    1. Advertising

  2. You need myNum to be declared global:

    def setNum():
    global myNum
    myNum = 10
    print myNum

    Apart from that, its usually considered bad style using global variables -
    better use this:

    def calcNum():
    myNum = 10
    return myNum

    myNum = calcNum()


    --
    Regards,

    Diez B. Roggisch
     
    Diez B. Roggisch, Nov 1, 2004
    #2
    1. Advertising

  3. Jean-Daniel Gamache

    Peter Hansen Guest

    Peter Hansen, Nov 2, 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. hui
    Replies:
    2
    Views:
    3,096
  2. chutsu
    Replies:
    6
    Views:
    876
    Antoninus Twink
    Jun 3, 2009
  3. Andy B.
    Replies:
    0
    Views:
    302
    Andy B.
    Jun 19, 2009
  4. Sam Kong
    Replies:
    2
    Views:
    190
    Joel VanderWerf
    May 25, 2005
  5. kj
    Replies:
    3
    Views:
    122
    Eric Pozharski
    Mar 12, 2009
Loading...

Share This Page