On 10/13/2020 11:09 AM, Chris Angelico wrote:
On Wed, Oct 14, 2020 at 1:41 AM Ronald Oussoren via Python-ideas
<python-ideas@python.org> wrote:
#— (python 3.9)

import dataclasses

@dataclasses.dataclass
class MyException (Exception):
     a : int
     b : str


try:
     raise MyException(a=1, b="hello")

except MyException as exc:
     print(f"{exc.a=}, {exc.b=}”)

# —

I like it. Very elegant. Never thought of a dataclass as an exception.

That's the beauty of dataclasses not using metaclasses or inheritance: they can be used pretty much anywhere, with very few adverse interactions. All credit to attrs for this design.

Eric
_______________________________________________
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/5BQEIHSU35E3L5NK73JMPMUYHPZXRSUC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to