[Python-ideas] thoughts on generator.throw()

Gerald Britton gerald.britton at gmail.com
Wed Mar 18 18:10:52 CET 2009

Today I was reviewing changes in Python 2.5 and I noticed the
generator throw() method for the first time.  While thinking about
what it does and why, a question arose in my mind:

Why is it called "throw"?  (Yes, I know that Java and possibly other
languages use this keyword!)  In Python, we have long had a "raise"
statement to raise exceptions.  I would have thought that the
generator method would have been called "raise" as well.  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

is invalid syntax because raise is not a function.  Now, I can get around it:

    def raise_(exception):
         raise exception
    if 'foo' == 'bar' or raise_(FooBar):

I have a similar question about the "assert" statement.  It could
possibly benefit from being a function instead. Of course, changing
this would break lots of code, but maybe not any more than making
print a function as in 3.0.


Gerald Britton

More information about the Python-ideas mailing list