mudgen
7/13/2014 - 7:06 AM

Examples and results of variable scoping rules in Python 2.1, Python 2.5 and Ignition 7.6.

Examples and results of variable scoping rules in Python 2.1, Python 2.5 and Ignition 7.6.

"""
Summary:
Functions in Ignition 7.6 and earlier can access variables defined in outer functions but cannot access 
variables defined in module scope.

Functions in Python 2.1 can access variables defined in module scope but cannot access variables defined 
in outer functions.

Functions in Python 2.5 can access variables defined in module scope and can access variables defined in
outer functions.

The following functions were tested and show the scoping differences.
"""


name1 = "Joe"
def greet1():
  """
  This function was tested and works in Jython 2.1 and in Jython 2.5
  This function was tested and does not work in Ignition 7.6 because it references the "name1" variable
  that is defined in the module scope.
  """
  print name1

name2 = "Sara"
def greet2():
  """
  This function was tested and works in Jython 2.1 and in Jython 2.5
  This function was tested and does not work in Ignition 7.6 because it references the "name2" variable
  that is defined in the module scope.
  """
  def greet3():
    print name2
  greet3()
  
def greet4():
  """
  This function was tested and does not work in Jython 2.1 because the nested greet5 function tried to
  access the name3 variable that is defined in the outer function.
  This function was tested and works in Jython 2.5.
  This function was tested and works in Ignition 7.6
  """  
  name3 = "Bob"
  def greet5():
    print name3
  greet5()