Why TypeError when there are too many / too few arguments in a Python function call

I'm having trouble understanding why Python creates TypeErrorwhen you provide arguments that are not part of the method signature.

Example:

>>> def funky():
...    pass
... 
>>> funky(500)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: funky() takes no arguments (1 given)

I thought if it was because it was *argsexpected, Noneor []as part of the no-arg function, that there was a leaking abstraction , so I looked at it.

What i found

A search on the page TypeErroron PEP-3102 found what turned out to be an excuse for one context in which TypeErrorbut I don't understand the excuse. The PEP example essentially indicates that functionality is basically a shortcut to if args: raise TypeError(). args- this is a non-empty list in this case, unlike an empty list ... which are the same. If I am not mistaken, and this is indeed an excuse, it may ValueErrorbe more appropriate. However, this will still be a kind of fuzzy abstraction, since this example is written in Python, which makes it more part of the implementation of a specific use case than a language function. Sort ofArgumentErrorit sounds a lot more relevant to me, which leads me to believe that there are some obvious explanations that I have missed regarding why TypeErrorit makes sense.

+3
source share
2 answers

n. m. n != m. , , - - - API. , , , , . ? , " ", . , .. , , , , " , ", .. - , .

PEP, if args: raise TypeError(...) , varargs * . , , varargs . *, , , , - ( , ).

TypeError: " " - / . , Python, " ". ( Python, ) : " ", , . TypeError not isinstance(x, expected), , ( , " " ) "is-parent" ) .

: , , , . , Python, , " " ( ) - . (, , abritary arity, * **.) , , , , , Python , , , . type(f) function, .

0

: def funky():... "funky" "". , (), , . , ; def funky (n): pass.

0

All Articles