1

I kind of know the difference between var == None and var is None, however my question is about the following condition check:

if var:
  action

Is the above equivalent to:

if not var == None:
  action

or is it equivalent to:

if var is not None:
  action

Or does it depend on what is the current type of the "var" variable, how it performs the check?

Or does it have a completely different mechanic in the background?

Coming from that as a follow up what would happen if the value of "var" is actually 0 and it's of type float or int and I use the if var: construct?

Any insights about the inner workings of Python3 are much appreciated. Thanks!

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
kalintri
  • 21
  • 2
  • [This is the relevant section of the documentation](https://docs.python.org/3/library/stdtypes.html#truth-value-testing). Importantly: "By default, an object is considered true unless its class defines either a `__bool__()` method that returns False or a `__len__()` method that returns zero, when called with the object." – juanpa.arrivillaga Jan 30 '19 at 21:43

0 Answers0