PEP 285: Adding a bool type

Ralph Corderoy ralph at inputplus.demon.co.uk
Sun Apr 7 11:14:37 EDT 2002


Hi Max,

> > I think we all perfectly understand your opinion too and no matter how
> > many times you repeat it there will still be some of us that like to do
> > 
> >     a = b + is_movable(c
> 
> I can see that it is clever, but I don't get why it is smart. Isn't it 
> just another way of writing?:
> 
>      if is_movable(c):
>          a = b + 1

No.  BTW, you snipped a closing parenthesis there.

It's equivalent to

    a = b
    if is_movable(c):
        a += 1

or

    if is_movable(c):
        a = b + 1
    else:
        a = b

or

    a = b + is_movable(c) ? 1 : 0

but that last one isn't valid Python but does show a strong C
background is likely to make you familiar with this approach.

> If it is, I find that it is more obfuscation than expression. It's
> akind to using a magic value. My paraphrase of our code should really
> read something like::
> 
>      motionLength = 1
>      if is_movable(c):
>          a = b + motionLength

That doesn't cover `not is_movable(c)' again.

> Or your code should possibly have read::
> 
>      a = b + motionLength(c)
> 
> But probably I misunderstand you intentions.

The quantity being added to b isn't a length, it's 1 indicating that c
is movable.  Trying to name it anything other than what it is is
contortion for some ideal that doesn't work in this case.

Say you had a list of items and wanted to sort them to place those with
the most abilities first, with all abilities considered equal, and then
sorting on the item's name where two have the same number of abilities.

To calculate the major sort key you might do

    major = can_be_moved(o) + can_be_sized(o) + can_be_stacked(o) +
        can_be_coloured(o) + can_be_hidden(o)

as opposed to

    major = 0
    if can_be_moved(o):
        major += 1
    if can_be_sized(o):
        major += 1
    if can_be_stacked(o):
        major += 1
    if can_be_coloured(o):
        major += 1
    if can_be_hidden(o):
        major += 1

I think the former is way clearer, and not because it uses less
key-strokes, although brevity can aid clarity sometimes.

Cheers,


Ralph.




More information about the Python-list mailing list