> On 13 Oct 2020, at 16:08, jmwar...@gmail.com wrote: > > Steven, > Thanks for the responses. In response to your responses....: > Steven D'Aprano wrote: >> Hi jmward01, >> I'm not really sure what you mean by "boilerplate heavy objects". >> Boilerplate generally applies to the amount of source code you have to >> write. I don't think that a two line class (perhaps a couple of extra >> lines if you give it a docstring) justifies the name "boilerplate": >> class MySpecialException(Exception): >> pass >> >> In my experience, that covers the majority of custom exceptions I've >> needed. (More on this below.) >> As for "heavy objects", it's true that classes are moderately >> heavyweight: >> py> sys.getsizeof(Exception) >> 400 >> >> but the instances are relatively lightweight: >> py> sys.getsizeof(Exception('spam eggs')) >> 96 >> >> and if you're stressed out by that, you might be using the wrong >> language :-) >> So I'm not quite sure what specific part of this you are worried about: >> memory consumption or lines of code. > I am not particularly concerned with performance here. I am more worried > about code clarity, boilerplate that de-incentivizes a feature and hidden > values. The current exception mechanism encourages passing, effectively, > un-named lists of parameters or building boilerplate to create the equivalent > of named tuples (e.args[0] or building an __init__ that defines members). If > the only value is the name, why not just define it where you use it instead > of creating an intermediary class definition? I use string literals for state > names all the time instead of creating objects and importing them all over my > code because most of the time the mental overhead of imports and the like > isn't worth the ease and clarity of just passing a string. This is the same > argument I am making for most exceptions by suggesting this syntax.
Using exception.args or an explicit __init__ isn’t necessary: #— (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=}”) # — Ronald — Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/
_______________________________________________ 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/3JBQGA2PWRWA74ECJQROJVQTD4J5IKC5/ Code of Conduct: http://python.org/psf/codeofconduct/