How to chain attribute lookups that might return None in Python?

前端 未结 6 956
小鲜肉
小鲜肉 2021-01-07 17:07

My problem is a general one, how to chain a series of attribute lookups when one of the intermediate ones might return None, but since I ran into this problem t

6条回答
  •  走了就别回头了
    2021-01-07 17:41

    My best shoot to handle middle-way null attributes like this is to use pydash as sample code on repl.it here

    import pydash
    title = pydash.get(soup, 'head.title.string', None)
    

提交回复
热议问题