[Tutor] funny true or false

Gonçalo Rodrigues op73418@mail.telepac.pt
Fri May 9 07:22:02 2003


----- Original Message -----
From: "ali" <rmangaliag@slu.edu.ph>
To: "Tutor" <tutor@python.org>
Sent: Friday, May 09, 2003 9:29 AM
Subject: [Tutor] funny true or false


> yesterday, i tried playing with some if's and while's... then i tried
> this... and i thought it was funny.... just want you to comment on this...
>
> >>> if True:
> >>>     print "test"
> >>> # of course, this will print "test"
> >>> if False:
> >>>     print "test"
> >>> # and of course, this wont...
> >>> # but when i do this...
> >>> True = 0
> >>> False = 1
> >>> # and run the same if's above
> >>> if True:
> >>>     print "test"
> >>> # things change... :) weird...
> >>> if False:
> >>>     print "test"
> >>> # and of course, this will print "test"
>
> isn't this weird and may lead to confusion (of course, is your using True
or
> False as variables and not as constants)...
>
> what if you inadvertently assigned a value to True or False... it may lead
> to confusion...
>
> i even tried to assign a list to True or False and it worked...
>
> maybe it's just me... :)
>
> ali
>

Yes. Currently, True, False and None are just builtins (names bound in the
builtin namespace), so you can shadow them. Future versions of Python are
slated to have them all as keywords, so you won't be able to shadow them
anymore. I believe in the upcoming Python 2.3, if you try to assign to None,
the interpreter will snarl with a warning.

All the best,
G. Rodrigues