Python3.8_获取函数形参内容

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.args)
# 打印结果
['orderSource', 'inventoryId', 'pageNum']

#取形参值
print(result.defaults) 

#打印结果 ('All', None, 1)

 

 
posted @ 2021-06-25 15:27  error015  阅读(461)  评论(0)    收藏  举报