Which exception should I raise on bad/illegal argument combinations in Python?

前端 未结 7 1359
北荒
北荒 2020-12-02 04:09

I was wondering about the best practices for indicating invalid argument combinations in Python. I\'ve come across a few situations where you have a function like so:

<
7条回答
  •  醉话见心
    2020-12-02 04:43

    I think the best way to handle this is the way python itself handles it. Python raises a TypeError. For example:

    $ python -c 'print(sum())'
    Traceback (most recent call last):
    File "", line 1, in 
    TypeError: sum expected at least 1 arguments, got 0
    

    Our junior dev just found this page in a google search for "python exception wrong arguments" and I'm surprised that the obvious (to me) answer wasn't ever suggested in the decade since this question was asked.

提交回复
热议问题