On Fri, Sep 03, 2021 at 09:32:26AM -0700, Guido van Rossum wrote:
> The question is, would anyone ever want to make a distinction between the
> two in *real* code? I find it unlikely that someone would write
>
> try:
> sum(x, y, z)
> except TypeError:
> ...
Not for sum specifically, but yes, there are similar cases where I
would. This is especially useful when using feature-detection rather
than version-checking.
E.g. `min` and `max` now take a key parameter, but old versions didn't:
# This is in 2.4
>>> min(['a'], key=len)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: min() takes no keyword arguments
A more recent example is math.gcd() which used to only take exactly two
arguments but now takes an arbitrary number of arguments. We can use
feature-detection to distinguish the two cases:
gcd() # in Python 3.5
--> TypeError: gcd() takes exactly 2 arguments (0 given)
gcd() # in Python 3.9
--> returns 0
Looking into the future, it would be useful to be able to distinguish
between:
- passing the correct number of arguments but wrong values
(ValueError or similar) or wrong types (TypeError or AttributeError);
- and passing the wrong number of arguments, or invalid keywords.
That second case is currently a TypeError. I proposed a new subclass of
TypeError for that purpose, ParameterError:
https://mail.python.org/archives/list/[email protected]/message/MXPCNEAWXWJPOHB3DC3QW3S3ZPOFSM4Q/
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/WVOF77TL6AHCGUHIY4L66EH6HWWOW7SU/
Code of Conduct: http://python.org/psf/codeofconduct/