Pythonic way to check if something exists?

后端 未结 6 1410
北恋
北恋 2020-12-12 14:06

This is pretty basic but I was coding and started wondering if there was a pythonic way to check if something does not exist. Here\'s how I do it if its true:



        
6条回答
  •  春和景丽
    2020-12-12 14:07

    What about the negation?

    if not var:
        print 'nope it does not'
    

    Though this is to see if the var is false / none and would blow up if var is not defined.

    To see if var is defined:

    try:
        var
    except NameError:
        print 'not defined'
    

提交回复
热议问题