FW: PEP 8: exception style
PEP 8 doesn't express any preference between the two forms of raise statements: raise ValueError, 'blah' raise ValueError("blah")
I like the second form better, because if the exception arguments are long or include string formatting, you don't need to use line continuation characters because of the containing parens. Grepping through the library code, the first form is in the majority, used roughly 60% of the time.
Should PEP 8 take a position on this? If yes, which one?
I we had to pick one, I would also choose the second form. But why bother inflicting our preference on others, both forms are readable so we won't gain anything by dictating a style. Raymond
[AMK]
PEP 8 doesn't express any preference between the two forms of raise statements: raise ValueError, 'blah' raise ValueError("blah")
I like the second form better, because if the exception arguments are long or include string formatting, you don't need to use line continuation characters because of the containing parens. Grepping through the library code, the first form is in the majority, used roughly 60% of the time.
Should PEP 8 take a position on this? If yes, which one?
[Raymond Hettinger]
I we had to pick one, I would also choose the second form. But why bother inflicting our preference on others, both forms are readable so we won't gain anything by dictating a style.
Ongoing cruft reduction -- TOOWTDI. The first form was necessary at Python's start because exceptions were strings, and strings aren't callable, and there needed to be _some_ way to spell "and here's the detail associated with the exception". "raise" grew special syntax to support that need. In a Python without string exceptions, that syntax isn't needed, and becomes (over time) an increasingly obscure way to invoke an ordinary constructor -- ValueError("blah") does exactly the same thing in a raise statement as it does in any other context, and transforming `ValueError, 'blah'` into the former becomes a wart unique to raise statements.
participants (2)
-
Raymond Hettinger
-
Tim Peters