python函数作用域

l:local

e:enclosing

g:global

b:buitl-in

函数变量有作用域 if else while 变量没有作用域

不能修改全局变量 若要修改需加global

a=int(23.22)  ##built-in
count=10 #global
def func():
      #global count  都是5
      count=5 #local 先打印5再打印10
      print count
func()
print count

a=int(23.22)  ##built-in
count=10 #global
def func():
      #global count
      count=5 #local
      def fun1():
          nonlocal count
          count=14
          print(count)
      fun1()
      print (count)
func()
print (count) ##ret 14 14 10

  

原文地址:https://www.cnblogs.com/howhy/p/7732872.html