【发布时间】:2021-11-30 15:04:48
【问题描述】:
哪一个是 Python 中合适的 基本错误,用于限制在函数中传递的 参数数量。
我想将其限制为 2 个参数,如下所示:
def func(*args):
if len(args) == 1:
# Do one thing
elif len(args) == 2:
# Do another thing
else:
## raise an error ##
【问题讨论】:
-
为什么不写两个函数呢?
-
会脱离函数逻辑
-
那么我认为这个 SO stackoverflow.com/a/1964247/9479128 可能会通过继承 TypeError 来回答你的问题
-
哦,太好了,@Nathan Furnal 感谢您提供的信息,尽管我不想子类化。
标签: python error-handling