[Python-ideas] Arguments to exceptions
Ken Kundert
python-ideas at shalmirane.com
Mon Jul 3 19:58:21 EDT 2017
All,
My primary concern is gaining access to the components that make up the
messages. I am not hung up on the implementation. I just proposed the minimum
that I thought would resolve the issue and introduce the least amount of risk.
Concerning MRAB's idea of making the named arguments attributes, I am good with
it. I considered it, though I was thinking of using __getattr__(), but thought
that perhaps it was a step to far for the BaseException.
-Ken
On Tue, Jul 04, 2017 at 12:04:16AM +0100, MRAB wrote:
> Maybe exceptions could put any keyword arguments into the instance's
> __dict__:
>
> class BaseException:
> def __init__(self, *args, **kwargs):
> self.args = args
> self.__dict__.update(kwargs)
>
> You could then raise:
>
> raise NameError('name {!a} is not defined', name='foo')
>
> and catch:
>
> try:
> ...
> except NameError as e:
> print('{}: nicht gefunden.'.format(e.name))
More information about the Python-ideas
mailing list