Raising objects

Alex Martelli aleax at aleax.it
Thu May 1 18:39:58 EDT 2003


Steven Taschuk wrote:
   ...
> What really bothers me about the
>     raise ValueError, 'n < 0'
> syntax is that the relationship between the two parts is precisely
> that between the class of an object and that object's individual
> state -- ValueError is the type of error, and 'n < 0' is state
> which specifies what *this* error is.  The normal way to express
> that relationship in Python (the one obvious way to do it?) is
> with classes and instances of them, such as with
>     raise ValueError('n < 0')
> which makes much more sense to me.  I think you and I are in
> concord here; in your desiderata above you'd have the comma syntax
> be equivalent to the instantiating syntax.  (As far as I can know,
> it already is, actually.)

Yes, indeed it is.


> I don't see any benefit to the comma syntax these days, other than
> backwards compatibility.  Indeed, it seems to me slightly harmful,
> not so much because it looks like raising a tuple (though it
> does), but because it creates a false analogy between the raise
> syntax and the except syntax:
>     >>> foo = 'bar'
>     >>> try:
>     ...     raise ValueError, foo
>     ... except ValueError, e:
>     ...     pass
> The syntactic parallel suggests that e is foo; but in fact e is an
> instance of ValueError which contains foo.

Yes, this is a good point.  The parallel is indeed misleading.


>> > Hm... maybe I should write a PEP.
>> 
>> Yes, I agree you should -- nothing as major as this is gonna happen
>> without a lot of PEPping.
> 
> I might yet do so.  (For now I'm still changing my views too
> rapidly; I'll look at it again when I've settled down.)

We probably do need to aim at a world where (except for backwards
compatibility) one raises an instance (of some subclass of
NewException), period -- getting out of the current "raise klas"
equalling "raise klas()" confusion.  But how to get there ain't
obvious.  Perhaps a first step might be to patch all of the standard
library to eradicate the "raise klas" and "raise klas, blah" uses.
Then optional warnings for raising strings or anything else except
instances of some subclass of Exception (but we can't do that until
the standard library's conforming to the new preferred ways).  Then
make the warnings default to on.  Then...


Alex





More information about the Python-list mailing list