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

bart.c bartc at freeuk.com
Sun Jul 11 05:57:27 EDT 2010


"rantingrick" <rantingrick at gmail.com> wrote in message 
news:1b285203-33f6-41fb-8321-381c154bc8ed at w12g2000yqj.googlegroups.com...
> Let me tell you folks about a recent case of culo rojo i experianced
> whilst creating a customized bin packer with Python. First i want to
> say that i actually like the fact that i can do this..
>
> py> a = []
> py> if a:
> ...     do something
>
> Instead of this
>
> py> if len(a) > 0:
> ...     do something

Or perhaps: if len(a):
 ...

> Ok but the buck stops with integers. Why? you ask in amazing
> befuddlement...Well I happened upon this atrocity when creating
> variables that hold indexes into a python list.

>    choiceIdx1 = None
>    choiceIdx2 = None
>    if array[0] meets condition this condition:
>        choiceIdx1 = 0
>    for i in range(len(array)):
>        if array[i] meets this condition:
>            choiceIdx2 = i
>            break
>    if choiceIdx1 and not choiceIdx2:

> BUT THAT WONT WORK BECAUSE OF CRAPPY INTEGER BOOLEAN DEFAULTS! So i
> had to do this crap...!

You can also blame the 0-based indexing in Python.

I'm not sure what would be the more fundamental change: changing over to 
1-based indexing, or for 0 to be True (probably the opposite meaning to 
every other language).

> array = [c1,c2,c3,c4,c5,c6,...]
> while looping:
>    choiceIdx1 = ()
>    choiceIdx2 = ()
>    if array[0] meets condition this condition:
>        choiceIdx1 = (0,)
>    for i in range(len(array)):
>        if array[i] meets this condition:
>            choiceIdx2 = (i,)
>            break
>    if choiceIdx1 and not choiceIdx2:

>
> Seems kinda dumb to build a tuple just so a conditional wont blow
> chunks! This integer bool-ing need to be fixed right away!

So, you're simply trying to signal whether a value is in the range 0 or 
more, or not? That doesn't sound difficult: start with -1, then test whether 
it's >=0.

But you're trying a boolean test where you expect None=False, and 0,1,2, etc 
= True. While this would save typing in the condition, you're introducing 
extraneous stuff elsewhere which makes it harder to read, such as (i,) just 
to store an index.

-- 
Bartc 




More information about the Python-list mailing list