Main.LocalVariables History

Hide minor edits - Show changes to markup

January 28, 2010, at 03:01 PM MST by 129.82.44.241 -
Added lines 1-33:

(:source lang=python:)

"""Local variables"""

  1. When you create a local variable inside a function, it only exists
  2. inside the function, and you cannot use it outside.
  3. also, modifying a parameter does not affect the variable that
  4. was passed into the function
  5. for example:

def test_locality(var1, var2) :

    print "in the method 'test_locality'"
    print "var1", var1, "var2", var2
    print "after modifying the variables"
    var1 = 100
    var2 = 'python'
    print "var1", var1, "var2", var2

var1 = 1 var2 = "a string" print "var1", var1, "var2", var2 test_locality(var1, var2) print "back in main" print "var1", var1, "var2", var2

  1. although the variables have the same name, they are different
  2. instances

(:sourceend:)