concise code (beginner)

Diez B. Roggisch deets at nospam.web.de
Wed Sep 5 06:20:27 EDT 2007


bambam 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

for method in (DevClass.read1, DevClass.read2, ...):
  for dev in devs:
      try:
        method(dev)
      except:
        print execption
        remove dev from devs

Diez



More information about the Python-list mailing list