python Python 2.1,Python 2.5和Ignition 7.6中的变量作用域规则的示例和结果。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python 2.1,Python 2.5和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()
  



以上是关于python Python 2.1,Python 2.5和Ignition 7.6中的变量作用域规则的示例和结果。的主要内容,如果未能解决你的问题,请参考以下文章

Python教程(2.1)——第一个Python程序

2.1Python变量编码注释

python Python 2.1,Python 2.5和Ignition 7.6中的变量作用域规则的示例和结果。

Python学习笔记__2.1章 调用函数

python基础(1.8-2.1)

Python学习笔记(2.1)函数参数练习