[Tutor] if n == 0 vs if not n
Vern Ceder
vceder at canterburyschool.org
Tue Oct 6 02:40:25 CEST 2009
Dave Angel wrote:
>>> Now in this case where it is only used as boolean checks which would be
>>> the most pythonic way if writing these checks?
>>>
>>
> 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.
>
> DaveA
True, I took the OP's statement that they were to be used "only as
boolean checks" to mean that there was no type mixing going on.
Personally, I would say that checking a list or string for equality (or
lack thereof) with 0 is even less "preferable". ;)
Otherwise, one would at least prefer "if n != 0" to "if not n == 0", I
would think.
Cheers, Vern
--
This time for sure!
-Bullwinkle J. Moose
-----------------------------
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137
More information about the Tutor
mailing list