m="monday"
s="sunday"
def foo(**kws):
loc = locals()#把locals()方法
print(loc)
print(kws) # 这样试试
for i in kws:
print(i,"=",kws[i])
foo(s=s)
print("********************")
# 现在的打印效果some code <class 'str'> monday
# 希望的打印效果 t <class 'str'> monday
import inspect
def foo2(s):
arg_name = [n for n, v in inspect.currentframe().f_back.f_locals.items()
if v is s][0]
print(f'{arg_name} -> {s}')
foo2(m)
foo2(s)
python学习笔记2-获取参数的名字
最新推荐文章于 2024-10-14 23:40:45 发布
本文深入探讨了Python中函数参数的处理,通过`locals()`方法展示了函数内部变量的字典表示,并利用`inspect`模块获取函数参数名称。示例代码展示了如何在函数调用中获取和打印参数名及值,对于理解Python函数内部工作原理有帮助。
摘要由CSDN通过智能技术生成