[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code

On Mon, 18 Sep 2017 10:54 pm, Rustom Mody wrote:

> The operation
> x == True
> for true(!)/proper booleans x is a no-op
> because True == True is True
> and False == True is False
> And there are no other (proper) booleans

Yes, this exactly!

> However because anything else can be bool-ish even though not boolean
> you need the bool(x) to effect the mapping:

You don't need to explicitly call bool() since Python does it for you. All
you're doing is what Python does anyway. One might as well write:


    '%s' % str(x)

    tuple((1, 2))



    integers = map(lambda x: int(x), values)  # instead of just map(int, values)

and other signs of the programmer who lacks fluency in the language. I've made
nearly all of those errors, but I've learned from other, better coders and
recognised my mistakes.

I'm sure I still have plenty of mistakes remaining, but at least I've stopped
writing map(lambda x: int(x), values) and other rookie errors.

> {None, 0, "" {}, []} ? False
> Everything_else ? True

Your list of false-like ("falsey") values is incomplete. By very strong
convention, all empty containers should compare false. That includes those you
show, plus sets, frozensets, tuples, arrays, bytearrays, various Abstract Base
Classes in the collections.abc module, etc.

Likewise other zero values: 0.0, 0j, Decimal(0), Fraction(0), etc.

There may be others.

> This mapping is neither obvious nor trivial

By convention, it is. Of course you can design your class' __bool__ any way you
like, and make it as non-obvious and confusing as they like. But you shouldn't.

Empty sequences and collections should be false. Non-empty sequences and
collections should be true.

Values which represent "nothing" in some sense -- zero, empty strings, empty
collections, None -- should be falsey. Values which represent "something" --
non-zero numbers, non-empty strings, non-empty collections, arbitrary
objects -- should be truthy.

Of all people, Rustom, you should appreciate this. There's a whole field of
mathematics that builds up arithmetic starting from the empty set, defining it
as equivalent to zero, and the set of empty sets as one. (I simplify, of
course, but I'm sure you know that.)

> And one could argue that leaving python to implicitly make [] (say) into False
> should be documented

As it is.



?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.