# 局部变量与全局变量

2018/05/04 18:11

name='lhf'

name='“lfe”

def change_name():
global name
name='帅了一比'
print('change_name',name)

name = "adf"
def change_name():
name='帅了一比'
print('change_name',name)
print(name)
'帅了一比'        # 因为name='帅了一比'是在global后面赋值的，所以把全局变量改了

NAME = ["产品经理","廖波湿"]
def qupengfei():
name = "自己"
global NAME
print('我要搞', NAME)
qupengfei()

NAME = '海风'

def huangwei():
name = "黄伟"
print(name)
def liuyang():
name = "刘洋"
print(name)
def nulige():
name = '沪指花'
print(name)
print(name)
nulige()
liuyang()
print(name)

5.事例，看竖排排版排序,当出现函数名加括号[huangwei()]这种情况时才需要进入到指定函数的过程，依次往下进行。

name = "刚娘"

def weihou():
name = "陈卓"              #最后取得的值就是三个print的结果
def weiweihou():
global name
name = "冷静"

weiweihou()
print(name)

print(name)
weihou()
print(name)
# 刚娘
# 陈卓
# 冷静

6.新概念，nonlocal（上一级的变量）

name = "刚娘"

def weihou():
name = "陈卓"
def weiweihou():
gnonelocal name   # nonlocal，指定上一级变量，如果没有就继续往上直到找到为止
name = "冷静"

weiweihou()
print(name)

print(name)
weihou()
print(name)
# 刚娘
# 冷静
# 刚娘

0
0 收藏

0 评论
0 收藏
0