Comparing True False Confusion

I have some confusion over the test values ​​assigned to False, True

To check the value of True, we can simply simply

a = True if (a): 

how about false?

 a=False if (a) <--- or should it be if (a==False), or if not a ? 
+6
source share
3 answers

In the Python Style Guide :

For sequences (rows, lists, tuples), use the fact that empty sequences are false.

 Yes: if not seq: if seq: No: if len(seq) if not len(seq) 

[..]

Do not compare booleans with True or False using ==.

 Yes: if greeting: No: if greeting == True: Worse: if greeting is True: 
+21
source

use not :

 if not a: .... # If a is the empty value like '', [], (), {}, or 0, 0.0, ..., False # control flow also reach here. 

or is False :

 if a is False: .... 
+3
source

Check if the value is true:

 if a: pass 

To check if a value is invalid:

 if not a: pass 

However, not a: there is True (and true) for values ​​other than False , for example. None , 0 and empty containers.

If you want to check if the value is True or False (although you usually don’t), try:

 if a is True: pass 

or

 if a is False: pass 

Edit: to check the value True or False it seems that you should use if isinstance(a, bool) and a and if isinstance(a, bool) and not a

0
source

Source: https://habr.com/ru/post/953639/


All Articles