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?
Jay 5 20720
NavyJay wrote: 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 = value
for 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
> 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?
maybe i don't fully understand your qn but why don't you let __init__ of
your class raise an exception and then catch it in the outer for loop and
continue. something like:
for i in ...
try:
foo()
except:
continue
Vikram
Exactly the answer I was looking for! 12 hours of straight programming
tends to fog ones mind. Thanks for making it clear!
Jay
Vikram wrote: 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?
maybe i don't fully understand your qn but why don't you let __init__ of your class raise an exception and then catch it in the outer for loop and continue. something like:
for i in ... try: foo() except:
except (ValueError, TypeError), error: continue
Use something like the above, (always expect certain kinds of errors)
lest you accidentally capture an real attempt to stop the program
such as the exception a Control-C causes.
-Scott David Daniels Sc***********@Acm.Org
Or better yet, define your own string/class exception to catch your
errors. In my code, things can break in more than a few ways. In each
case I catch the exception(s) specific to that piece of code, print a
warning message to the user at sys.stdout and raise a new exception to
be caught by my "wrapper" exception clause. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: user |
last post by:
I have gotten properties to respond correctly, but
when I try to do it in __init__:
class foo:
def getter(self):
return "hello"
def __init__(self):
|
by: Urs Eichmann |
last post by:
Upon startup of my ASP.NET 2.0 application, I check if the application
configuration is in an acceptable state inside an override of
HttpApplication.Init(). If not, I raise an exception from this...
|
by: dickinsm |
last post by:
Here's an example of a problem that I've recently come up against for
the umpteenth time. It's not difficult to solve, but my previous
solutions have never seemed quite right, so I'm writing to...
|
by: mattbennett |
last post by:
Hi,
I'm new to python, and having problems instantiating classes. My main method has a couple of lines like this:
debug = False
stats_d = {"cpu":,"foo":,"bar":,"baz":}
...
|
by: Christoph Zwerschke |
last post by:
What is the best way to re-raise any exception with a message
supplemented with additional information (e.g. line number in a
template)? Let's say for simplicity I just want to add "sorry" to every...
|
by: 7stud |
last post by:
When I run the following code and call super() in the Base class's
__init__ () method, only one Parent's __init__() method is called.
class Parent1(object):
def __init__(self):
print...
|
by: Maese Fernando |
last post by:
Hi,
I'm getting an odd error while trying to call the __init__ method of a
super class:
BaseField.__init__(self)
TypeError: unbound method __init__() must be called with BaseField
instance...
|
by: Ryan Krauss |
last post by:
I am trying to call a parent's __init__ method from the child's:
class ArbitraryBlock(InnerBlock):
def __init__(self, codelist, noout=False, **kwargs):
InnerBlock.__init__(self, codelist,...
|
by: Jerry Hill |
last post by:
On Thu, Aug 7, 2008 at 11:11 AM, Ryan Krauss <ryanlists@gmail.comwrote:
This works for me just fine:
class InnerBlock(object):
def __init__(self, codelist, noout=False, **kwargs):...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |