python - 以编程方式确定函数所需的参数数量 - Python

标签 python parameters function

我正在创建一个简单的命令行实用程序,并使用字典作为一种 case 语句,其中的关键字链接到其相应的功能。这些函数都需要不同数量的参数,因此目前要检查用户是否输入了每个函数所需的正确数量的参数,我将所需的数量以 {Keyword:(FunctionName, AmountofArguments) 的形式放入字典 case 语句中}.

当前的设置工作得很好但是我只是想知道是否有办法确定函数中所需的参数数量并且我的谷歌尝试到目前为止没有返回任何值(value)但我知道如何args 和 kwargs 可能会搞砸这样的命令,因为它们允许的参数数量是无限的。

最佳答案

inspect.getargspec() :

Get the names and default values of a function’s arguments. A tuple of four things is returned: (args, varargs, varkw, defaults). args is a list of the argument names (it may contain nested lists). varargs and varkw are the names of the * and ** arguments or None. defaults is a tuple of default argument values or None if there are no default arguments; if this tuple has n elements, they correspond to the last n elements listed in args.

关于python - 以编程方式确定函数所需的参数数量 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/741950/

相关文章:

python - 如何为公共(public) RSA/DSA key 生成 PEM 序列化

java - 为什么某个 IDE 不允许我为 JComboBox 添加参数?

javascript - 使用 javascript 在 html 中调用外部函数时出现问题

Jquery:以编程方式居中元素

javascript - 从回调函数中获取值

python - 计算滑动范围内的点数

python - 困境: Should I learn Seaside or a Python framework?

python - python文本框中文本和滚动条的自动滚动

python ,argparse : enable input parameter when another one has been specified

linux - bash CLI 参数设置脚本变量吗?