Python3.8,win10
# 获取以下方法的形参内容
def func(orderSource=‘All‘, inventoryId=None, pageNum=1): ...
1、仅获取形参的默认值
# 魔法方法
print(func.__defaults__)
# 返回结果如下,仅返回了形参的默认值
(‘All‘,None, 1)
2、获取形参名+默认值
使用内置模块 import inspect
import inspect def func(orderSource=‘All‘, inventoryId=None, pageNum=1): ... result = inspect.getfullargspec(func)) print(result) # 打印结果 FullArgSpec(args=[‘orderSource‘, ‘inventoryId‘, ‘pageNum‘], varargs=None, varkw=None, defaults=(‘All‘, None, 1), kwonlyargs=[], kwonlydefaults=None, annotations={}) # 取形参名 print(result[0]) # 打印结果 [‘orderSource‘, ‘inventoryId‘, ‘pageNum‘] #取形参值 print(result[3]) #打印结果 (‘All‘, None, 1)
原文:https://www.cnblogs.com/error015/p/14931121.html