[Python-ideas] thoughts on generator.throw()
Antoine Pitrou
solipsis at pitrou.net
Wed Mar 18 18:20:11 CET 2009
Gerald Britton <gerald.britton at ...> writes:
>
> But then I
> saw that it would have been impossible to implement since "raise" is a
> Python keyword. *Then* I wondered why "raise" is a keyword and not a
> function. If it were a function you could use it easily in places
> where today you cannot:
>
> if 'foo' == 'bar' or raise(FooBar): # only proceed if 'foo'
> equals 'bar' otherwise raise FooBar exception
I find this horrible, awfully Perlish. Non-local control transfers should stick
out clearly when reading source code, not be hidden at the end of a conditional.
As for why raise is a keyword, I think there are several explanations:
- raise is a control flow operation, as are "return", "continue", "break" and
others.
- raise has to create a traceback capturing the current frame stack, which is
easier with a dedicated bytecode.
- raise should be decently fast, which is easier with a dedicated bytecode.
> I have a similar question about the "assert" statement. It could
> possibly benefit from being a function instead.
I think the point is that assert is entirely a no-op when the interpreter is run
with "-O", while there would be a significant overhead if it was a regular
function call.
But I agree that the situation is less clear-cut than with the raise statement.
Regards
Antoine.
More information about the Python-ideas
mailing list