concise code (beginner)

James Stroud jstroud at
Wed Sep 5 20:17:52 CEST 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()

My keen sense of pattern recognition tells me that all of your read's 
follow the same naming pattern--or was that just an accidental naming 
coincidence on your part for the sake of example?

for i in xrange(number_of_reads):
   for dev in devs:
       _reader = getattr(dev, 'read%d' % i)
     except Exception, e:
       print e


James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

More information about the Python-list mailing list