名称空间与作用域及函数补充
目录
名称空间
名称空间是存放变量名与变量值绑定关系的地方
名称空间的分类
1.内置名称空间
python解释器提前定义好的,我们可以直接使用
eg:
print()
input()
type()
file.read()
file.write()

2.全局名称空间
在py文件中编写的代码运行产生的名字都会存到全局名称空间
password = '123456' # 变量名password存入全局名称空间
def func(): # 函数名func存入全局名称空间
pass
if True:
username = input('please enter your username>>>:') # 变量名username存入全局名称空间
for line in file: # 变量名line存入全局名称空间
pass
while True:
username = input('please enter your username>>>:') # 变量名username存入全局名称空间
password = input('please enter your password>>>:') # 变量名password存入全局名称空间
3.局部名称空间
函数体代码运行产生的都是局部名称空间
def register():
username = input('please enter your username>>>:') # 变量名username存入局部名称空间
password = input('please enter your password>>>:') # 变量名password存入局部名称空间
存活周期
- 内置名称空间
python解释器运行,内置名称空间产生
python解释器关闭,内置名称空间消失 - 全局名称空间
py文件开始运行,全局名称空间产生
py文件运行结束,全局名称空间消失 - 局部名称空间
函数体代码开始运行,局部名称空间产生
函数体代码运行结束,局部名称空间消失

名字的查找顺序
在查找变量名或函数名等名字的时候,一定要先确定该名字在哪个名称空间
1.如果在局部名称空间
查找顺序:局部名称空间 >>> 全局名称空间 >>> 内置名称空间
2.如果在全局名称空间
查找顺序:全局名称空间 >>> 内置名称空间
input = '我是全局名称空间中的input'
def index():
input = '我是局部名称空间中的input'
print(input)
index() # 调用函数,函数体就会执行,产生局部名称空间
print(input)
x = 8
'''通过注释让函数体代码不执行来发现局部名称空间的查找规律'''
def f1():
x = 88
def f2():
# x = 888
def f3():
# x = 8888
def f4():
# x = 88888
print(x) # 依次注释法f4,f3,f2,f1让函数体代码,找到规律
f4()
f3()
f2()
f1()
x = 8
'''通过注释让函数体代码不执行来发现局部名称空间的查找规律'''
def f1():
x = 88
def f2():
x = 888
def f3():
x = 8888
def f4():
print(x) # 特例,检测语法的时候发现f4的局部名称空间有变量名x,查找的时候就只找f4的局部名称空间。
x = 88888 # 如果修改变量名x,就可以正常执行
f4()
f3()
f2()
f1()
作用域
作用域就是名称空间能够作用的范围
- 内置名称空间
程序任意阶段任意位置均可使用(全局有效) - 全局名称空间
程序任意阶段任意位置均可使用(全局有效) - 局部名称空间
一般情况下,只在所属的局部名称空间中有效(局部有效);
补充:也可以通过global与nonlocal关键字打破规则

global与nonlocal关键字
gloabl关键字可用于局部修改全局不可变类型,修改全局可变类型不需要global关键字
# 不可变类型
name = 'trump'
def modify_name():
# name = 'obama' # 并不能修改全局的name(值为字符串,不可变类型),而是在modify_name局部名称空间中创建了一个新的name
# 如果想要在局部名称空间中修改全局名称空间中的名字 那么需要使用关键字申明
global name # 修改的是全局name而不是在modify_name局部名称空间中创建新的name
name = 'obama'
modify_name()
print(name)
# 可变类型
dict1 = {'name': 'trump', 'age': '74', 'position': 'president'}
def delete():
dict1.pop('position') # 不需要global关键字
delete()
print(dict1)
# 局部名称空间嵌套的情况下
def func():
name = 'trump'
dict1 = {'name': 'trump', 'age': '74', 'position': 'president'}
def modify():
nonlocal name
name = 'obama' # 不可变类型需要使用关键字nonlocal
dict1['position'] = 'ex-president' # 可变类型不需要使用关键字nonlocal
modify()
print(name)
print(dict1)
func()
函数名的多种用法
用法1:函数名可以当做变量名赋值
def index():
print('from function index')
print(index) # <function index at 0x000001548A541EA0>
res = index # index赋值给res
print(res) # function index at 0x000001548A541EA0>
res() # from function index
用法2:函数名还可以充当函数的实参
def delete():
print(delete) # <function delete at 0x000001D5E0141EA0>
print('from delete‘s function') # from delete‘s function
def func(a):
print('from func') # from func
print(a) # <function delete at 0x000001D5E0141EA0>内存地址一致
a()
func(delete)
用法3:函数名还可以当做函数的返回值
def func():
print('from func') # from func
print(index) # <function index at 0x0000019830D5DC80>
return index # 将函数名当做返回值
def index():
print('from index') # from index
res = func() # res接收函数名
print(res) # <function index at 0x0000019830D5DC80>
res() # index()
用法4:函数名可以作为容器类型的元素
# 容器类型(列表、元组、字典)的内部可以存档多个元素的数据类型
def index():
print('from index')
dict1 = {'func1': 'delete', 'func2': index}
print(dict1) # {'func1': 'delete', 'func2': <function index at 0x00000254ABEE1EA0>}
dict1['func2']() # from index
函数的嵌套
def modify():
func()
print("from modify function")
return
def func():
modify()
print("from func function")
return
modify() # 报错 maximum recursion depth exceeded
def two_max(a, b):
'''返回两个数中的较大值'''
if a > b:
return a
return b
print(two_max(100, 666))
def many_max(a, b, c, d):
'''返回四个数中的最大值'''
res1 = two_max(a, b)
res2 = two_max(res1, c)
res3 = two_max(res2, d)
return res3
print(many_max(66, 333, 888, 777))
用函数编写员工管理系统
staff_info_dict = {}
# 三个小功能
def func1():
'''输入员工信息界面'''
staff_name = input('请输入您的姓名>>>:').strip()
staff_age = input('请输入您的年龄>>>:').strip()
staff_salary = input('请输入您的薪资>>>:').strip()
return staff_name, staff_age, staff_salary
def num_func(a):
'''输入工号'''
staff_num = input(f'请输入您的工号用于{a},例如1001,1002>>>:').strip()
return staff_num
def print_func(staff_num):
print('''
员工工号:%s
员工姓名:%s
员工年龄:%s
员工工资:%s
''' % (staff_num.zfill(8), staff_info_dict[staff_num]['name'], staff_info_dict[staff_num]['age'], staff_info_dict[staff_num]['salary']))
# 七个主体功能
def register():
'''添加员工信息'''
tag2 = True
while tag2:
staff_num = num_func('添加员工信息') # 调用工号输入函数
if staff_num.isdigit():
if staff_num in staff_info_dict:
print('工号已存在,不能重复,请重新输入')
else:
print('温馨提示,请记住您输入的工号,工号查看和修改工资的凭证;如有忘记,主界面输入4查看所有员工信息')
staff_name, staff_age, staff_salary = func1() # 调用员工信息输入输入函数
staff_data = {}
staff_data['name'] = staff_name
staff_data['age'] = f'{staff_age}岁'
staff_data['salary'] = f'{staff_salary}元'
staff_info_dict[staff_num] = staff_data
print(f'工号为{staff_num.zfill(8)}的员工信息添加成功,添加的信息如下')
print_func(staff_num) # 调用打印员工信息函数
tag2 = False
else:
print('温馨提醒,工号只能为纯数字,例如1001,1002,2222')
def search():
'''查看单个员工信息'''
print('查看单个员工信息'.center(30, '-'))
tag3 = True
while tag3:
staff_num = num_func('查看信息') # 调用工号输入函数
if staff_num in staff_info_dict:
print_func(staff_num) # 调用打印员工信息函数
cmd = input('是否继续查看其他员工信息,输入y继续,其他退出>>>:')
if cmd == 'y':
pass
else:
tag3 = False
else:
print('工号不存在,您可以仔细想一想,有无限的尝试次数')
def modify_salary():
'''修改单个员工工资'''
print('修改单个员工工资'.center(30, '-'))
count = 0
while count < 3:
staff_num = num_func('修改工资') # 调用工号输入函数
if staff_num.isdigit():
count += 1
if staff_num in staff_info_dict:
print_func(staff_num) # 调用打印员工信息函数
staff_info_dict[staff_num]['salary'] = input('请输入修改后的工资>>>:').strip() + '元'
temp_salary = staff_info_dict[staff_num]['salary']
print(f'工号为{staff_num.zfill(8)}的员工工资已经修改为{temp_salary},修改信息如下')
print_func(staff_num) # 调用打印员工信息函数
break
else:
print('工号不存在,请重新输入')
else:
print('温馨提醒,工号只能为纯数字,例如1001,1002,2222')
else:
print('三次机会使用完毕')
def search_all():
'''查看所有员工信息'''
print('查看所有员工信息'.center(30, '-'))
for staff_num in staff_info_dict:
print_func(staff_num) # 调用打印员工信息函数
def delete_staff():
'''删除单个员工信息'''
print('删除单个员工信息'.center(30, '-'))
tag4 = True
while tag4:
count2 = 0
while count2 < 3:
staff_num = num_func('删除信息') # 调用工号输入函数
count2 += 1
if staff_num.isdigit():
if staff_num in staff_info_dict:
print(f'您正在删除工号为{staff_num.zfill(8)}的信息为')
print_func(staff_num) # 调用打印员工信息函数
cmd = input("删除后不可撤回,输入'yes'确定删除,如果要删除其他的员工信息,输入其他返回主界面>>>:")
if cmd == 'yes':
staff_info_dict.pop(staff_num)
print(f'工号为{staff_num.zfill(8)}的员工信息删除成功')
count2 = 3
tag4 = False
else:
print(f'取消删除工号为{staff_num.zfill(8)}的员工信息成功')
count2 = 3
else:
print('工号不存在,您还有两次机会')
else:
print('温馨提醒,工号只能为纯数字,例如1001,1002,2222')
else:
print('退回主界面')
tag4 = False
def modify_staff():
'''修改单个员工信息'''
print('修改单个员工信息'.center(30, '-'))
count = 1
while count < 4:
staff_num = num_func('修改员工信息') # 调用工号输入函数
count += 1
if staff_num.isdigit():
if staff_num not in staff_info_dict:
print('工号不存在,不能重复,请重新输入')
else:
print('温馨提示,请记住您输入的工号修改员工信息')
staff_name, staff_age, staff_salary = func1() # 调用员工信息输入输入函数
staff_data = {}
staff_data['name'] = staff_name
staff_data['age'] = f'{staff_age}岁'
staff_data['salary'] = f'{staff_salary}元'
staff_info_dict[staff_num] = staff_data
print(f'工号为{staff_num}的员工信息修改成功,修改后的信息如下')
print_func(staff_num) # 调用打印员工信息函数
count = 4
else:
print('温馨提醒,工号只能为纯数字,例如1001,1002,2222;您可以仔细想一想,有三次的尝试次数')
def exit_func():
'''退出员工系统'''
global tag # 修改全局tag的布尔值
tag = False
print('欢迎下次使用')
func_dict = {1:register, 2:search, 3:modify_salary, 4:search_all, 5:delete_staff, 6:modify_staff, 7:exit_func}
tag = True
while tag:
print('''
---------------------欢迎使用员工管理系统-------------------------
----------------1.添加员工信息-----------------------------------
----------------2.查看特定员工-----------------------------------
----------------3.修改员工薪资-----------------------------------
----------------4.查询所有员工-----------------------------------
----------------5.删除特定员工-----------------------------------
----------------6.修改员工信息-----------------------------------
----------------7.退出员工系统-----------------------------------
''')
chioce = input('现在位置为主界面,请输入功能编号').strip()
if chioce.isdigit():
chioce = int(chioce)
if chioce in func_dict:
func_dict[chioce]()
else:
print('请好好输入功能编号,只有1-7')
else:
print('请好好输入纯数字,不要乱填')

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?