*blink* That's neat.

Fredrik Lundh effbot at telia.com
Tue May 30 10:29:38 CEST 2000


Stephen Hansen wrote:
> While writing a function, I curiousely typed this into the Interpreter:
> 
> >>> if Case is not 0 or 1:
>         ....
> 
> etc..and it worked.. This was rather surprising to me :)

"worked" as in "did not give you a syntax error", you mean? ;-)

unless guido has abused his time machine again, your interpreter
treats this as:

    if (Case is not 0) or 1:
        ....

which probably isn't what you really wanted to do.

or maybe you really wanted to do this?

> if Case is not 0 or Case is not 1:
> if Case != 1 or Case != 0

same thing here -- unless Case is a really strange object, it cannot
be 0 and 1 at the same time, so one side of the "or" is always true.

assuming that you really meant "and", how about:

    if Case not in (0, 1):
        ...

otherwise,

    if 1:

is probably more efficient ;-)

also note that "is" compares object identity, not object value.  afaik,
the language specification doesn't guarantee that all integers having
the value "1" also have the same object identity.

btw, usability research has shown that "and" and "or" are about the
worst names you can use for these operators.  how about renaming
them in Py3K? ;-)

</F>





More information about the Python-list mailing list