#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Beginner Python questions!


    I'm trying to teach myself Python. Right now I'm using a book called Inventing Your Own Computer Games with Python which is free online.

    I started to get stuck on one particular concept and although I tried to carry on to see if it would make more sense later, the rest of the chapter stopped making sense too.

    This is the code in question and then it breaks it down to explain specific concepts:
    http://inventwithpython.com/dragon.py

    Code:
    # This block doesn't run until funky() is called:
    def funky():
        # We create a local variable named "spam"
        # instead of changing the value of the global
        # variable "spam":
        spam = 99
        # The name "spam" now refers to the local
        # variable only for the rest of this
        # function:
        print(spam) # 99
    # A global variable named "spam":
    spam = 42
    print(spam) # 42
    # Call the funky() function:
    funky()
    # The global variable was not changed in funky():
    print(spam) # 42
    So the book is trying to explain global and local scope which I thought I understood but then the final part of that code confuses me. If they call the function, why doesn't spam=99? Is it because of the formatting? Should it be

    Code:
    funky()
        print(spam)
    for it to be 99?

    I really appreciate the help!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    print changes only the world external to python. It does not change value or scope of variables.


    If there is an assignment to a variable of any valid name anywhere within a function that variable is local to that function.


    (Unless you tell python otherwise with a global or nonlocal statement.)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0
    I'm not sure that I understand.

    As I said, I am a complete beginner so I'm struggling quite a lot with the vocab.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    507
    Rep Power
    33
    Strings are immutable so you can not change a string, but instead you always create a new one. Note how the id is the same inside and outside the function until the variable is changed (actually it points to a new block of memory), and the id changes after the variable changes. You use global (or return the variable from the function) so everything refers to the same memory address-->see here for an explanation of global, and print the id's if you are not sure which variable is where..
    Code:
    def funky(spam):
        # We create a local variable named "spam"
        # instead of changing the value of the global
        # variable "spam":
        print("inside function, before change", id(spam))
        spam = 99
        # The name "spam" now refers to the local
        # variable only for the rest of this
        # function:
        print(spam) # 99
        print("inside function, after change", id(spam)) ## now different
    
    # A global variable named "spam":
    spam = 42
    print(spam) # 42
    print("before function", id(spam))
    # Call the funky() function:
    funky(spam)
    # The global variable was not changed in funky():
    print(spam) # 42
    print("after function", id(spam))  ## stilll the original memory address
    ##
    ##----------------  change here and it is also a new variable
    spam=99
    print(id(spam))  ## now different
    Last edited by dwblas; August 18th, 2013 at 09:50 PM.

IMN logo majestic logo threadwatch logo seochat tools logo