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

后端 未结 6 996
执笔经年
执笔经年 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:17

    How about a two-liner?

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

    Denser, less readable, but you really save two keypresses!

提交回复
热议问题