Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if x:, vs if x == True, vs if x is True

Apologies if this has been asked before, but I have searched in vain for an answer to my exact question. Basically, with Python 2.7, I have a program running a series of geoprocessing tools, depended on what is reqested via a series of True/False variables that the user adjusts in the script e.g.

x = True  if x:     run function 

However, I have now discovered that x does not need to be literally "True" for the function to run. For example:

In: x = True     if x:         print True  Out: True  In: x = 123     if x:         print True  Out: True  In: x = 'False'     if x:         print True  Out: True  In: x = False     if x:         print True  Out:  

So any value other than False appears to evaluate to True, which would not be the case for if x == True or if x is True. Seeing as PEP 8 strongly recommends only using the if x: variant, can anybody explain why this behaviour occurs? It seems that if x: is more a test for "if x is not False" or "if x exists". With that in mind, I believe I should be using if x is True: in this case, despite what PEP 8 has to say.

like image 359
ssast Avatar asked Dec 06 '13 09:12

ssast


People also ask

What is correct data type of X if X true?

The truth value of all integers except 0 is true (in this case, the 2). if x == True: , however, compares x to the value of True , which is a kind of 1 .

What does X true mean?

if you use if x ,it means it has to evaluate x for its truth value. But when you use x ==True or x is True . It means checking whether type(x)==bool and whether x is True. attention : x is True is no equal to bool(x)==True.

Is True same as true in Python?

In Python (and many other languages), there is True , and there are truthy values. That is, values interpreted as True if you run bool(variable) . Similarly, there is False , and there are falsy values (values that return False from bool(variable) ).

What is the difference between x == true and X is true?

if you use if x ,it means it has to evaluate x for its truth value.But when you use x ==True or x is True .It means checking whether type (x)==bool and whether x is True. when you use x is True , you are checking the id of x and True.

Does x need to be literally “true” to run a function?

Basically, with Python 2.7, I have a program running a series of geoprocessing tools, depended on what is reqested via a series of True/False variables that the user adjusts in the script e.g. However, I have now discovered that x does not need to be literally "True" for the function to run. For example:

How to check if x is true or not true in Python?

a=2 if a==True: print (a,"True") else: print (a,"Not True") output> (2, Not True) if you use if x ,it means it has to evaluate x for its truth value.But when you use x ==True or x is True .It means checking whether type (x)==bool and whether x is True.

Is it safe to write if x is true or false?

Now, if you've arranged that x is necessarily one of the objects True or False, then you can safely write if x. If you've arranged that the "trueness" of x indicates whether or not to perform the operation, regardless of type, then you can safely write if x. Where you can write that you should prefer to do so, since it's cleaner to read.


1 Answers

The following values in Python are false in the context of if and other logical contexts:

  • False
  • None
  • numeric values equal to 0, such as 0, 0.0, -0.0
  • empty strings: '' and u''
  • empty containers (such as lists, tuples and dictionaries)
  • anything that implements __bool__ (in Python3) to return False, or __nonzero__ (in Python2) to return False or 0.
  • anything that doesn't implement __bool__ (in Python3) or __nonzero__ (in Python2), but does implement __len__ to return a value equal to 0

An object is considered "false" if any of those applies, and "true" otherwise, regardless of whether it's actually equal to or identical with False or True

Now, if you've arranged that x is necessarily one of the objects True or False, then you can safely write if x. If you've arranged that the "trueness" of x indicates whether or not to perform the operation, regardless of type, then you can safely write if x. Where you can write that you should prefer to do so, since it's cleaner to read.

Normally, if it is allowed for x to take the value True then you're in one of those two cases, and so you would not write if x is True. The important thing is to correctly document the meaning of x, so that it reflects the test used in the code.

Python programmers are expected to know what's considered true, so if you just document, "runs the function if x is true", then that expresses what your original code does. Documenting it, "runs the function if x is True" would have a different meaning, and is less commonly used precisely because of the style rule in PEP8 that says to test for trueness rather than the specific value True.

However, if you wanted the code to behave differently in the case where x is an empty container from the case where it is None, then you would write something like if x is not None.

like image 187
Steve Jessop Avatar answered Sep 22 '22 01:09

Steve Jessop