Difference between if and if is not None

后端 未结 4 986
广开言路
广开言路 2020-12-31 10:14

In writing some XML parsing code, I received the warning:

FutureWarning: The behavior of this method will change in future versions.  Use specific \'len(elem         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-31 10:59

    if obj is not None test whether the object is not None. if obj tests whether bool(obj) is True.

    There are many objects which are not None but for which bool(obj) is False: for instance, an empty list, an empty dict, an empty set, an empty string. . .

    Use if obj is not None when you want to test if an object is not None. Use if obj only if you want to test for general "falseness" -- whose definition is object-dependent.

提交回复
热议问题