I have a simple for-loop, which instantiates a class object each iteration. As part of my class constructor, __init__(), I check for valid input settings. If there is a problem with this iteration, I want to abort the loop, but equivalently 'continue' on in the for-loop.
I can't use 'break' or 'continue' in a class method, nor can I return a boolean value from __init__() to check for errors within the for-loop. How would I be able to stop the current iteration and continue with the next after reporting an error?
You have the right idea in the subject header: raise an exception. Something along the lines of this:
class Foo(object):
def __init__(self, value):
if value > 10:
raise ValueError("Value must be under 10, was %s." % value)
else:
self.value = valuefor value in [1, 4, 10, 7, 15, 13, 6, 3]:
try:
obj = Foo(value)
except ValueError, ex:
print str(ex)
continue
# Do stuff with obj here
--
http://mail.python.org/mailman/listinfo/python-list
