全局变量调用:想要在自定义的函数中使用全局变量,就得要在
函数用关键字global声明,然后就可以对全局变量进行修改。
嵌套函数中的变量的调用:要在嵌套的变量中,使用nonlocal的声明
'''
num = 1
def fun1():
    global num  # 需要使用 global 关键字声明
    print(num)
    num = 123
    print(num)
fun1()
print(num)

def outer():
    num = 10
    def inner():
        nonlocal num   # nonlocal关键字声明
        num = 100
        print(num)
    inner()
    print(num)
outer()

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-05-18
  • 2022-01-20
  • 2021-09-02
  • 2021-11-15
猜你喜欢
  • 2021-11-15
  • 2021-07-04
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-11-15
  • 2021-12-26
相关资源
相似解决方案