How best to write this try/except block?

Jeff Youel jeff at jeffyouel.com
Wed Apr 3 12:10:53 EST 2002


"Roy Smith" <roy at panix.com> wrote in message
news:a8fba8$ln8$1 at panix2.panix.com...
> I've got a dictionary d, which may or may not have a value I'm looking
> for.  If it does, I want to do a few things.  If it doesn't, I want to
> do a few other things.  I could write:
<snip>

You can avoid the exception handler altogether with:

    value = d.get(key, None)
    if value:
        foo = stuff(value)
    else:
        foo = otherStuff(key)


Jeff





More information about the Python-list mailing list