Raise X or Raise X()?

Irmen de Jong irmen.NOSPAM at xs4all.nl
Sun Mar 11 16:37:26 EDT 2012


On 11-3-2012 20:04, bvdp wrote:
> Which is preferred in a raise: X or X()? I've seen both. In my specific case I'm dumping out of a deep loop:
> 
> try:
>   for ...
>     for ...
>       for ...
>         if match:
>            raise StopInteration()
>          else ...
> 
> except StopInteration:
>    print "found it"

"raise X" is a special case of the 3-args raise. Effectively it just raises an instance
of X which is constructed with an empty argument list. Therefore, "raise X()" is
equivalent, as far as I know.

See http://docs.python.org/reference/simple_stmts.html#the-raise-statement

Irmen




More information about the Python-list mailing list