全局变量,全局都可以调用的变量
局部变量,在子程序中可以调用的变量
name = ‘w‘ def text(): name = ‘l‘ print(name) text() print(name)
global调用全局变量,且可以重新赋值,如果没有则不能重新赋值
text()决定调用顺序
name = ‘w‘ def text(): global name name = ‘l‘ print(name) def text2(): print(name) text2() text() print(name)
global调用全局变量,且可以重新赋值,如果没有则不能重新赋值
name = ‘w‘ def text(): global name name = ‘l‘ print(name) def text2(): print(name) text() text2() print(name)
没有global不能重新赋值,但对可变类型,可以对内部元素进行操作
name = [‘1‘,‘2‘] def a(): name.append(‘3‘) print(name) a() print(name)
全局变量都大写,局部变量都小写
nonlocal指上一级变量
name = ‘w‘ def abc(): name = ‘a‘ def bcd(): nonlocal name name = ‘b‘ bcd() print(name) print(name) abc() print(name)
原文地址:https://www.cnblogs.com/wangleiyifan/p/9248263.html
时间: 2024-10-07 07:46:01