Strange PEP8 recommendation on comparing Boolean values to True or False

前端 未结 6 1251
你的背包
你的背包 2020-12-06 04:54

At the end of python PEP8 I\'m reading:

  • Don\'t compare boolean values to True or False using ==

    Yes:   if greeting:
    No:    if g         
    
    
            
6条回答
  •  甜味超标
    2020-12-06 05:39

    I usually name my boolean variables after the pattern IsName, so in you case IsGreeting. This makes the check read if IsGreeting/if not IsGreeting, which is very intuitive.

    The ambiguities you are describing with if not are the result of using non-boolean types in boolean comparisons. This should usually be avoided, as it is very confusing.

提交回复
热议问题