python全局变量与局部变量详解
#python中的变量:全局变量和局部变量
#在很多语言中,在声明全局变量的时候,都喜欢把全局变量的名称定义为大写
#定义学生人数为100
student_number = 100
#学校名称
school_name = ‘guangzhou university’
“””\
获取全局变量:学生人数student_number的值
“””
def getstudentnumber():
return student_number
“””\
重置全局变量:学生人数student_number值
“””
def setstudentnumber(num):
global student_number
student_number = num
“””\
获取全局变量:学校名称school_name的值
“””
def getschoolname():
return school_name
“””\
重置全局变量:学校名称school_name值
“””
def setschoolname(name):
global school_name
school_name = name
def changevalue():
name = ‘北京大学’
number = 90
print(‘全局变量student_number = ‘ , getstudentnumber())
print(‘全局变量school_name = ‘ , getschoolname())
print(‘#######################################’)
print(‘局部变量namber = ‘ , number)
print(‘局部变量name = ‘ , name)
print(‘#######################################’)
print(‘改变全局变量值…’)
print(‘#######################################’)
setstudentnumber(number)
setschoolname(name)
print(‘全局变量student_number = ‘ , getstudentnumber())
print(‘全局变量school_name = ‘ , getschoolname())
if __name__ == ‘__main__’:
changevalue()