#6.2 The scope of the variable is the accessible scope of the variable, also known as the namespace. The first time
#给变量赋值时, Python creates variables. The scope of the variable is determined by the position where the variable was assigned for the first time.
#作用域类型
#一个程序通常包含了变量, functions, and other statements. Variables and functions involve an accessible scope. Variables and functions in a program
#要么在当前文件中定义, or Python is pre-defined. The function and program files are divided into different scopes.
#在同一个作用域中. The variable name is unique. In different scopes, the same variable name also represents a different variable.
#在pyton中作用域范围可以分为内置作用域, file scopes, function nesting scopes, and local scopes.
#内置作用域和文件作用域被称为全局作用域.
#函数嵌套作用域有时, also known as a local scope.
#根据作用域的范围大小, variables and functions outside the scope can be used directly within the scope, whereas variables in scope
#不能在作用域外直接使用.
#根据作用域范围, variable names are usually divided into two types: global variables and local variables.
#a global variables;
A =10
#参数b, is a local variable inside the function Add.
def add (b):
#c是函数add内的本地变量, A is a global variable outside the function.
C=a+b
Return C
#调用函数;
Print (Add (5))
#在函数运行的过程中, a add, is a global variable. b c is a local variable. Built-in function print ()
#作用域外的变量和作用域内的变量名称相同时, follow the local "precedence" principle, at which time the outside scope is masked
#---scope isolation principle.
#例如:
a=10
Def show ():
#赋值, create a local variable a
a=1000
Print (' int show (): A= ', a)
#调用函数, the result of the observation is that the local variable masks the global variable.
Show ()
#将上面的函数稍作修改:
#赋值, create global variable A
a=10
Def show ():
#在此先打印a的值 to see if global variables are used.
Global A
Print ("A=", a)
#赋值, create a local variable.
a=100
Print ("A=", a)
#调用函数;
Show ()
The scope of a Python variable