Silly questions about True and False

Peter Hansen peter at engcorp.com
Mon Dec 20 13:20:38 EST 2004


drs wrote:
> I just upgraded my Python install, and for the first time have True and
> False rather than 1 and 0.  I was playing around at the command line to test
> how they work (for instance, "if 9:" and "if True:" both lead to the
> conditional being executed, but True == 9 -> False, that this would be true
> was not obvious to me -- "True is True" is True, while "9 is True" is false
> even though 9 evaluates to True.)  

What do you mean by "9 evalutes to True"?  That's not the
case.  bool(9) does evaluate to True, and that's effectively
what "if 9:" is doing...

Anyhow, in doing my tests, I accidentally
> typed
> 
> 
>>>>False = 0
> rather than
> 
>>>>False == 0
> 
> and I lost the False statement.

To get it back, you should do "del False".  Remarkably,
this actually just removes the local name False that
you created which was "shadowing" the builtin name,
allowing the builtin name (which you didn't change)
to be seen again.

Note that this name you are creating is actually *local*
to the module you are in, which at the interactive
prompt is called __main__.  Thus you are not changing
False from the point of view of any other module, or
of the Python internals.  They are (for the most part)
still getting it from the builtin module.


> which seems to put False back to False, but this seems weird.
>>>>1 = 0
> 
> throws an error (can't assign to literal), why doesn't False = 0 throw the
> same error?  

False is not a constant, it's merely a name.  1 and 0 are
constants.  You can't change a constant, but you *can*
"rebind" a name (that is, attach it to something else).
That's all you're doing here.

> Also, why doesn't False = 0 make
>>>>1 == 2
> 0
> Instead of False?

Because such comparisons are all effectively doing a bool()
which continues to return the builtin False.

-Peter



More information about the Python-list mailing list