concise code (beginner)

Francesco Guerrieri f.guerrieri at gmail.com
Wed Sep 5 12:29:58 CEST 2007


On 9/5/07, bambam <david at asdf.asdf> wrote:
>
> I have about 30 pages (10 * 3 pages each) of code like this
> (following). Can anyone suggest a more compact way to
> code the exception handling? If there is an exception, I need
> to continue the loop, and continue the list.
>
> Steve.
>
> -----------------------------------
> for dev in devs
>     try:
>         dev.read1()
>     except
>         print exception
>         remove dev from devs
>
> for dev in devs
>     try:
>         dev.read2()
>     except
>         print exception
>         remove dev from devs
>
> for dev in devs
>     try:
>         dev.read3()
>     except
>         print exception
>         remove dev from devs
>
> etc.
>


well the first thought is:  why don't you put all the calls in a single for
loop?
for dev in devs:
try:
    call the methods on dev (instead of explicitly elencating all of them
there could and should be other solutions more mantainable)
except:
    flag dev as unusable

Modifying the sequence you are iterating over can be tricky.


francesco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070905/1789a92d/attachment.html>


More information about the Python-list mailing list