[Python-bugs-list] [ python-Bugs-421533 ] testing for != with or

noreply@sourceforge.net noreply@sourceforge.net
Fri, 04 May 2001 17:09:58 -0700


Bugs item #421533, was updated on 2001-05-04 16:42
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=421533&group_id=5470

Category: None
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: testing for != with or

Initial Comment:
#Windows98
#Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32

#please run this brief program:

s='ABCDE'
uneq=' != '
eq=' == '
t1='A'
t2='C'

# this loop works as expected
for char in s:
    if char != t1:
        print char + uneq + t1
    else:
        print char + eq + t1
        
print

# this loop NEVER prints eq?!?
for char in s:
    if char != t1 or char != t2:
        print char + uneq + t1 + ' or ' + t2
    else:
        print char + eq + t1 + ' or ' + t2

print

# this loop works as expected
for char in s:
    if char == t1 or char == t2:
        print char + eq + t1 + ' or ' + t2
    else:
        print char + uneq + t1 + ' or ' + t2

# end program

As you can see, when testing for inequality in the second loop, the correct branch in NEVER taken! The first loop shows != works, the third loop shows or works, but when they are combined the test is always true? Looks like maybe a parser error? (Just a guess)

Here is a sample output:
A == A
B != A
C != A
D != A
E != A

A != A or C # Big trouble here
B != A or C
C != A or C # and here
D != A or C
E != A or C

A == A or C
B != A or C
C == A or C
D != A or C
E != A or C


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-05-04 17:09

Message:
Logged In: YES 
user_id=31435

I can't imagine why you think this is a Python bug.  Given 
any character c whatsoever, at least one of (c != 'A') and 
(c != 'C') must be true, hence the "or" of those clauses 
must always be true.  In particular, when c is 'A' the c !
= 'C' clause is true, and when c is 'C' the c != 'A' clause 
is true.  You're getting exactly what you asked for, but 
that what you asked for is useless isn't Python's fault 
<wink>.

Perhaps you wanted "and" instead of "or"?


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=421533&group_id=5470