Is `if x:` completely equivalent to `if bool (x) True:`?

For the small test environment we are writing, I am trying to provide some useful features.

One of them should be equivalent to if x: but if it is completely equivalent to if bool(x) is True: then I could provide only one function to check if x is True: and if x:

Is denial of this equivalent? if bool(x) is False: equal to if not x: :?

+5
source share
3 answers

if x: completely equivalent to testing for a logical truth value, yes.

From the if documentation documentation :

He selects only one of the sets, evaluating the expressions one by one until a true value is found (see the Logical operations section for determining true and false)

where the Boolean operations section describes how truth is determined. The bool() function follows the same rules:

Returns a boolean value, that is, one of the values ​​True or False. x is converted using the standard validation procedure.

Documentation of standard types contains a section on verification of property rights :

Any object can be checked for true, for use in the if or while state or as an operand of boolean operations below.

not just inverts the same meaning of truth; therefore, if x is considered to be the above rule, not x returns False and True otherwise.

Be careful: in Python 2, the built-in names False and True can be masked by setting the global:

 >>> True = False >>> True False 

and thus, t113 tests may be deceived by this reassignment. Python 3 makes the keywords False and True .

You do not need to use the utility function bool(x) is True or bool(x) is False . All you need is bool(x) and not bool(x) , since they already create True and False objects. bool() and not cannot return anything else using is True or is False , this is extremely redundant.

Last but not least, try not to reinvent the test wheel. The standard Python library comes with the unittest library; it has the functions assertTrue and assertFalse , and the implementation of these functions uses only if and if not .

+6
source

Yes. They are equivalent. The rules used by if are the same as those used by bool . not just inverts these values ​​without changing the logic for determining likelihood or falsity.

+2
source

in addition to @ Martijn’s answer, if what you are building is some kind of class, you can determine what behavior you want to have in case of verifying the truth, for example ìf x by defining the __bool__ function.

 >>> class A: pass >>> a=A() >>> if a: print("this instance class is truthish") this instance class is truthish >>> a is True False >>> bool(a) is True True >>> 

The default behavior for a user-defined class must always be true in a truth test, to change this, simply define __bool__ ( __nonzero__ in python 2) so that it matches the class semantics, for example, for example:

 >>> class Switch: def __init__(self, state="off"): self.state = state def __bool__(self): if self.state == "on": return True return False >>> x=Switch() >>> if x: print("this instance class is truthish") >>> x is True False >>> bool(x) is True False >>> x.state="on" >>> if x: print("this instance class is truthish") this instance class is truthish >>> x is True False >>> bool(x) is True True >>> 
+1
source

All Articles