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

Cameron Simpson cs at zip.com.au
Mon Jul 12 22:30:43 EDT 2010


On 12Jul2010 17:36, Paul Rubin <no.email at nospam.invalid> wrote:
| Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
| > This is why virtually all low-level languages treat 0 as a false ...
| 
| OK, but is Python a low-level language,

Not particularly, but true/false and Boolean login are low level ideas.
If it works well in a low level language (where all you might have are
ints of various flavours and thus some equivalence notion is required),
why would you _change_ the conventions in a high level language without
a compelling reason? The more commonality in concepts there are, for
all that they are just conventions, the easier it is remember how to
do things.

| and if not, why are low-level
| languages appropriate examples to follow?

They may not be in any inherent sense, but they are examples and if the
usage doesn't cause pain are they inappropriate?

Steven:
| >> if myInt <> 0:
| >
| > I would argue against that. Why do you, the coder, care about the 
| > specific details of treating ints in a boolean context? The int type 
| > itself knows, leave the decision to it.
[...snip...]

This I'm only halfway with. I, the coder, _must_ know. The "if"
statement has an intent, and I need to know that "if myInt:" matches my
intent. Of course, using the right idioms it will match a lot of the
time. But I've certainly been bitten by writing:

  if myDict:

intending:

  if myDict is not None:

but also getting:

  if myDict != {}:

Clearly the fault was mine, but I, the coder, _must_ care about the
details of using non-Booleans in a Boolean context.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

It is Texas law that when two trains meet each other at a railroad crossing,
each shall come to a full stop, and neither shall proceed until the other has
gone.



More information about the Python-list mailing list