if not in python

None in python, False, empty string "", 0, empty list [] {} empty dictionary, empty tuple () are equivalent to False, namely:

not None == not False == not '' == not 0 == not [] == not {} == not ()

 

Conditions required to follow behind the statement if bool type data, i.e., True or False. However, if the data type is not bool, bool can be converted into a data type, the conversion process is implicit.
In Python, None, empty list [] {} empty dictionary, empty tuple (), and a series of 0 representative of non-empty and the objects are converted to False. In addition to other objects will be converted to True.
At the command if not 1, one will be converted to type bool True. It is not a non-logical operators, not 1 then the constant is False. So if statement if not under sentence 1, never executed.

 

Maybe you want to determine whether the case x None, but yet the `x == []` The judge also came in, this case can not be distinguished.

For pythoner if not x accustomed to such an approach must be clear that x is equal to None, False, empty string "", 0, empty list [] {} empty dictionary, empty tuple () not to judge you affect the job. 

 

For `if x is not None` and` if not x is None` wording, it is clear that the former is more clear, while the latter has the potential to readers misunderstood as `if (not x) is None`, it is recommended that the former, but this is Google Suggest style
 

in conclusion:

` IF None` the X-IS is not the best written, clear, error does not occur in the future to stick with such an approach.

If not x premise use such an approach are: x is equal be clear None, False, empty string "", 0, empty list [] {} does not affect empty dictionary, empty tuple (time) was determined for you Row.

Guess you like

Origin www.cnblogs.com/wuzm/p/11587183.html