Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > method for Exception base class to raise the exception in an expression?

Reply
Thread Tools

method for Exception base class to raise the exception in an expression?

 
 
Bengt Richter
Guest
Posts: n/a
 
      07-07-2005
Sometimes it could be handy, e.g., cutting short iteration:

>>> def raisex(self, *args):

... raise self.__class__(self, *args)
...
>>> Exception.raisex = raisex
>>> list(i for i in xrange(20) if i<10 or StopIteration().raisex())

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(i for i in xrange(20) if i<15 or StopIteration().raisex())

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> list(i for i in xrange(20) if i<25 or StopIteration().raisex())

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

a class method could be nicer once we have Exception as a new-style class.

Just a thought.

Regards,
Bengt Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raise X or Raise X()? bvdp Python 10 03-12-2012 04:08 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
raise Exception or raise Exception() ernest Python 2 11-14-2010 08:14 PM
raise or not to raise [Newbie] Jacol Python 5 02-05-2007 11:46 PM



Advertisments