Object-oriented philosophy

Antoon Pardon antoon.pardon at vub.be
Tue Sep 11 09:02:24 EDT 2018


On 07-09-18 22:08, Michael F. Stemper wrote:
>
>>  try
>>    id = xmlmodel.attrib['name']
>>  except KeyError:
>>    id = "constant power"
> Never mind! After I continued testing, I realized that the above
> should have been written as:
>
>   if 'name' in xmlmodel.attrib:
>     id = xmlmodel.attrib['name']
>   else:
>     id = "constant power"
>
> <facepalm>

How about:

id = xmlmodel.attrib.get('name', "constant power")





More information about the Python-list mailing list