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

Object-oriented philosophy


On 2018-09-07 14:51, Michael F. Stemper wrote:
> On 2018-09-06 16:00, MRAB wrote:
>> On 2018-09-06 21:24, Michael F. Stemper wrote:

>> A word of advice: don't use a "bare" except, i.e. one that doesn't
>> specify what exception(s) it should catch.

> In another case where I had a "bare exception", I was using it to see if
> something was defined and substitute a default value if it wasn't. Have
> I cleaned this up properly?
> 
>  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>

-- 
Michael F. Stemper
Always use apostrophe's and "quotation marks" properly.