[Tutor] if n == 0 vs if not n
davea at ieee.org
Tue Oct 6 02:23:29 CEST 2009
Vern Ceder wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Hi Sander,
> PEP 8, the "Style Guide for Python Code"
> http://www.python.org/dev/peps/pep-0008/ is pretty clear that the
> shorter version is preferable:
> if s:
> if n:
> if b:
> if not b:
> and so on...
> Sander Sweers wrote:
>> Hi Tutors,
>> I am going through someone's python script and I am seeing a lot of the
>> following boolean checks.
>> if not s == ""
>> if not n == 0
>> if b == True
>> if not b == True
>> All of these can be written without the == notation like "if n", "if s"
>> Now in this case where it is only used as boolean checks which would be
>> the most pythonic way if writing these checks?
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
The shorter version may be preferable, but it doesn't generally give the
same results. Without knowing the possible data, these substitutions
are not safe.
For example, replacing "if not n == 0" with "if n"
will give different results for values of "",  and so on. It
WILL work if you know that n is an int or float, however.
More information about the Tutor