面向过程中的局部变量(global)

时间:2022-02-06 20:08:32

1.school = 'oldboy.edu'

def change_name(name):

school = " Mage Linux"                  # 局部变量只在函数里生效

print(name, school)

name = 'Alex'

change_name(name)  #先调用

print(school)

2.

school = 'oldboy.edu'

def change_name(name):

global school           #设置全局变量

school = " Mage Linux"

print(name, school)

name = 'alex'

change_name(name)

print(school)

3.

school = 'oldboy edu.'
names = ['Alex', 'Jack', 'Rain']

def change_name():         # 字符串和整数不能修改, 字典和列表可以在局部变量中被修改

names[0] = 'alex'

print('inside func', name)

change_name()

print(name)