Can you explain where `except ParameterError` would produce better code
than the best current alternative? It seems to me that using this is like
stating "this code might be wrong" and would generally produce bad code.
For example if I wanted to use math.gcd on multiple values but I need to
support Python versions that only allow two arguments, then I'll just apply
it to two arguments at a time, maybe with functools.reduce. And in general
if I want to distinguish between versions I'd much rather just check the
version and thus assert "this code is correct for this version".

If you catch ParameterError, how do you know that it came directly from the
line you caught it from and not deeper within?

If you want to check that you're passing parameters properly, I suggest
writing code like this:

import inspect

signature = inspect.signature(func)
try:
    signature.bind(*args, **kwargs)
except TypeError as e:
    result = ???
else:
    result = func(*args, **kwargs)

That's basically code that I've actually used in practice.

If you want it to look prettier, you could probably write a decorator like
this:

@check_params
def func(...):
    ...

try:
    result = func(*args, **kwargs)
except func.ParameterError as e:
    result = ???
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TVWVCXAYIY4JKEJIGO7EPAYKLQG4IFAV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to