Sunday, May 27, 2012

Variable Scope

I know this was probably confusing, so let's go over it in detail.

Scope


Again, scope is the word we use to describe the ability to access certain variable names in a certain place in our code.  Languages vary on how scope works, but we'll stick with python.

So far, we've learned about two kinds of scope: function scope and global scope.  The two are separate from each other, but there are ways to cross over, but first let's explore what scope means for our programs.

Open a new file and save it where you want (I recommend calling it "scopeTest.py" and putting in a folder called "tests").  Lets start by creating a function and a few variables in various scopes.


globalVar = "This is global"


def myFunction():
  localVar = "This is local"
  print "myFunction - localVar:  " + localVar
  print "myFunction - globalVar: " + globalVar


myFunction()
print
print "global - globalVar: " + globalVar
print "global - localVar:  " + localVar




Can you figure out what will happen?  Let's run it and see if you're right.



Oops.  We couldn't access the variable localVar outside the function.  Ok so let's get rid of the line where we print that out

print "global - localVar:  " + localVar

Now let's make things a little trickier.  Let's change our function to this.


def myFunction():
  localVar = "This is local"
  globalVar = "This is local"
  print "myFunction - localVar:  " + localVar
  print "myFunction - globalVar: " + globalVar



Ok now let's run it and see what happens.



Surprised?  In one place, globalVar was one string, and in the other it was a different one at the same time.  Apparently, when you assign to globalVar inside the function, Python creates a new variable with function scope and hides or shadows the global-scope globalVar.  In order to access the global version of globalVar, we need to add this line underneath the def line.

global globalVar



That should do it.  Now run it.



There we go.  Now when we assign "This is local" to globalVar, we're assigning it to the global version.  You must do this whenever you want to assign to a global variable inside a function.

I hope that clarifies a few things for you about scope.  I know it's just another thing to get your mind around, but trust me: it's not that difficult once it clicks.  All these things make sense together; it just takes a little practice.


9 comments:

  1. You said... at the start at this page...

    "I know this was probably confusing, so let's go over it in detail."

    You need to learn how to correctly reference your work. How do you get to the first part ? I've been trying to find part one for a damn week !

    ReplyDelete
  2. Thanks for the tutorial, the "global" keyword used inside a function definition cured my local variable woes.

    ReplyDelete
  3. Are local function variables thread safe in python

    ReplyDelete
  4. Are local function variables thread safe in python

    ReplyDelete
  5. Thank you so much.I hope to really understand this information.The way you share stated everything useful content and content is quality awesome info.Python Online Training
    Learn Python Online

    ReplyDelete