Method Call in Exception

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 20 05:00:59 EDT 2006


Carl Banks wrote:

>  In fact a lot of Pythonistas recommend this way over the
> alternative, even when you don't have to.  For example, a lot of people
> recommend this:
> 
> try:
>     name = record.name
> except AttributeError:
>     name = "Freddy"
> 
> instead of this:
> 
> if hasattr(record,"name"):
>     name = record.name
> else:
>     name = "Freddy"
> 
> 

In this specific case, either of these would be better written as:

name = getattr(record, 'name', 'Freddy')



More information about the Python-list mailing list