code_junkie

Why is if not someobj: better than if someobj == None: in Python?

Nov 11th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. if not someobj:
  2. #do something
  3.  
  4. if someobj == None:
  5. #do something
  6.  
  7. >>> class A(object):
  8. ... def __repr__(self):
  9. ... return 'A()'
  10. ... def __nonzero__(self):
  11. ... return False
  12.  
  13. >>> class B(object):
  14. ... def __repr__(self):
  15. ... return 'B()'
  16. ... def __len__(self):
  17. ... return 0
  18.  
  19. >>> class C(object):
  20. ... def __repr__(self):
  21. ... return 'C()'
  22. ... def __cmp__(self, other):
  23. ... return 0
  24.  
  25. >>> class D(object):
  26. ... def __repr__(self):
  27. ... return 'D()'
  28. ... def __eq__(self, other):
  29. ... return True
  30.  
  31. >>> for obj in ['', (), [], {}, 0, 0., A(), B(), C(), D(), None]:
  32. ... print '%4s: bool(obj) -> %5s, obj == None -> %5s, obj is None -> %5s' %
  33. ... (repr(obj), bool(obj), obj == None, obj is None)
  34. '': bool(obj) -> False, obj == None -> False, obj is None -> False
  35. (): bool(obj) -> False, obj == None -> False, obj is None -> False
  36. []: bool(obj) -> False, obj == None -> False, obj is None -> False
  37. {}: bool(obj) -> False, obj == None -> False, obj is None -> False
  38. 0: bool(obj) -> False, obj == None -> False, obj is None -> False
  39. 0.0: bool(obj) -> False, obj == None -> False, obj is None -> False
  40. A(): bool(obj) -> False, obj == None -> False, obj is None -> False
  41. B(): bool(obj) -> False, obj == None -> False, obj is None -> False
  42. C(): bool(obj) -> True, obj == None -> True, obj is None -> False
  43. D(): bool(obj) -> True, obj == None -> True, obj is None -> False
  44. None: bool(obj) -> False, obj == None -> True, obj is None -> True
  45.  
  46. - Comparisons to singletons like None should always be done with
  47. 'is' or 'is not', never the equality operators.
  48.  
  49. Also, beware of writing "if x" when you really mean "if x is not None"
  50. -- e.g. when testing whether a variable or argument that defaults to
  51. None was set to some other value. The other value might have a type
  52. (such as a container) that could be false in a boolean context!
  53.  
  54. if not False:
  55. print "False is false."
  56. if not 0:
  57. print "0 is false."
  58. if not []:
  59. print "An empty list is false."
  60. if not ():
  61. print "An empty tuple is false."
  62. if not {}:
  63. print "An empty dict is false."
  64. if not "":
  65. print "An empty string is false."
  66.  
  67. >>> False == 0
  68. True
  69. >>> False == ()
  70. False
  71.  
  72. foo = bar and spam or eggs
  73.  
  74. if bar:
  75. foo = bar
  76. else:
  77. foo = eggs
  78.  
  79. if not spam:
  80. print "Sorry. No SPAM."
  81.  
  82. if spam == None:
  83. print "Sorry. No SPAM here either."
  84.  
  85. - Comparisons to singletons like None should always be done with
  86. 'is' or 'is not', never the equality operators.
Add Comment
Please, Sign In to add comment