Quicker way than “try” and “except” ? - Python

后端 未结 6 993
执笔经年
执笔经年 2021-01-14 07:39

I\'m often having code written as follows

try:
  self.title = item.title().content.string
except AttributeError, e:
  self.title = None

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 08:29

    Assuming the AttributeError happens on string:

    self.title = getattr(item.title().content, 'string', None)
    

提交回复
热议问题