[Zope] Another Python Script Question

Paul Winkler pw_lists at slinkp.com
Fri Dec 5 10:27:24 EST 2003


On Thu, Dec 04, 2003 at 08:29:20PM -0800, Dylan Reinhardt wrote:
> This idiom is most often used in the cases where different objects may
> support similar interfaces.  Instead of checking object type, you can
> just *try* using the interface you expect, ex:
> 
> ----
> for obj in context.objectValues()
>     try:
>         print obj.foo()
>     except: 
>         pass
> return printed
> ----

Hey now, let's not get the guy started off with bare excepts.
It's generally better to catch only the exceptions you anticipate.

for obj in context.objectValues()
    try:
        print obj.foo()
    except AttributeError: 
        pass
return printed

You don't want your script to hide 
UnexpectedDisastrousExceptionThatRuinsEverything ;-)

This is also discussed in the "easier-to-ask-forgiveness-than-permission"
section of the python cookbook.

-- 

Paul Winkler
http://www.slinkp.com
Look! Up in the sky! It's RASPUTIN HYPE GUNDAM!
(random hero from isometric.spaceninja.com)



More information about the Zope mailing list