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

B

Bengt Richter

Sometimes it could be handy, e.g., cutting short iteration:
... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top