How best to write this try/except block?

Andy Gimblett gimbo at ftech.net
Thu Apr 4 04:48:17 EST 2002


On Wed, Apr 03, 2002 at 05:10:53PM +0000, Jeff Youel wrote:

> You can avoid the exception handler altogether with:
> 
>     value = d.get(key, None)
>     if value:
>         foo = stuff(value)
>     else:
>         foo = otherStuff(key)

Nasty.  What if foo[key] is 0?  Or (evil cackle) False...  ;-)

Use has_key() or try...except...else instead.

-Andy

-- 
Andy Gimblett - Programmer - Frontier Internet Services Limited
Tel: 029 20 820 044 Fax: 029 20 820 035 http://www.frontier.net.uk/
Statements made are at all times subject to Frontier's Terms and
Conditions of Business, which are available upon request.





More information about the Python-list mailing list