integer >= 1 == True and integer.0 == False is bad, bad, bad!!!

Stephen Hansen me+list/python at ixokai.io
Sun Jul 11 02:22:27 EDT 2010


On 7/10/10 11:03 PM, rantingrick wrote:
> On Jul 11, 12:51 am, Stephen Hansen <me+list/pyt... at ixokai.io> wrote:
> 
>> You don't need to build a tuple. Just change the tests, to "if
>> choiceIdx1 is not None". Its a little more work, sure. But its not
>> enough that its even vaguely worth breaking the otherwise very useful
>> behavior of bool(0) == False.
> 
> 
> Hmm, i beg to differ. The if choice1 is not None and choice2 is not
> None is going to run off my screen and into my neighbors backyard
> swimming pool!

"if choiceIdx1 is not None and choiceIdx2 is not None:" is 54
characters. Your straw man argument fails. Even if this is in a method
of a class, that's only 64; even with another two levels of indentation
it still isn't longer then the 80 which is where some people consider
things "long" (I do not: I don't mind code > 100).

If you are so desperately concerned with space, then simply do:

    if (choiceIdx1, choiceIdx2) != (None, None):

Its only eleven characters longer.

Or, you can do:

    if None not in (choiceIdx1, choiceIdx2):

Its only two characters. You really can't honestly be making an argument
about two characters.

> If you *can* Stefen, 

My name is Stephen. This is the second time you've done this: if you
can't show even the vaguest respect and actually use my name and not a
mockery of it, then I'll just killfile you.

> show the class a "very useful" case for integer
> bool-ing? Please do so, although i doubt you can offer one. Maybe
> you'll offer this kinda kludgy stuff...
> 
> function(option=1) #instead of True
> function(option=0) #instead of False

Of course I won't offer that. If I wish a boolean flag, something which
can have only one of two states (although sometimes a three-state
'maybe' is useful, with None being the 'neither true nor false'), I'd
use the boolean.

There's numerous cases where "if x" where x is an integer is useful. A
counter is the simplest example. Say you're counting how many
watermelons are in your inventory there is, and you want to test if
there's any or not. "if watermelons" is the concise, readable,
understandable way to express this. Readability counts.

A boolean test in Python tests "something" verses "nothing", it doesn't
test Truth verses False

It is entirely consistent in this regard (except in the case of custom
classes which may decide to do strange things).

Zero is, clearly, nothing.

> This is another example of the damage integer booling does to your
> code and your mind.

Utter nonsense. No one does that unless they are coming from C or some
other language without a True/False and don't know about it, or if they
are using a codebase which is supporting a very old version of Python
before True or False were introduced.

But you're just going to argue endlessly, no doubt. Have you ever
actually considered the fact that your gut reaction might, I don't know,
be wrong or misguided? I've admitted when I'm wrong on more then one
occasion.

I really don't know why I bother.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100710/154ac75e/attachment-0001.sig>


More information about the Python-list mailing list