git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Object-oriented philosophy


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")