integer >= 1 == True and integer.0 == False is bad, bad, bad!!!
Steven D'Aprano
steve-REMOVE-THIS at cybersource.com.au
Mon Jul 12 23:55:43 EDT 2010
On Mon, 12 Jul 2010 20:22:39 -0700, Paul Rubin wrote:
> Steven D'Aprano <steve-REMOVE-THIS at cybersource.com.au> writes:
>> Writing the explicit tests:
>> if bool(myInt):
>> or even:
>> if myInt <> 0:
>>
>> are firmly in the same category. The only difference is that it is more
>> familiar and therefore comfortable to those who are used to languages
>> that don't have Python's truth-testing rules.
>
> It's like list.append returning None. It helps catch errors.
How?
>>> myInt = 'x'
>>> if myInt <> 0:
... print "myInt is not zero, safe to divide"
... print 42/myInt
...
myInt is not zero, safe to divide
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str'
What error did this catch?
--
Steven
More information about the Python-list
mailing list