Why do we need ==?

Hans Nowak wurmy at earthlink.net
Mon Dec 9 17:39:37 EST 2002


M wrote:

> Why do Python use the == syntax for comparison?
> 
> As assignment inside an if is not possible would not
> comparison could be done with just one =?
> 
> Why can't we do:
> 
> if a = 1:
>     print "Foo"
> 
> instead of
> 
> if a == 1:
>    print "Bar"
> 
> ???
> I think a 
> 
> if a equal 1:
>    print "Foo"
> 
> is better instead of == (which does not carry any clue as to what they
> do) if one single = is not possible...
> 
> ok, I'm not saying we should change Python right now. Just curious...

It seems that ancient Python (0.9.1) actually used '=' rather than '==' for 
comparisons. Using an executable compiled from 0.9.1 source:

(P:\bin) $ python-091.exe
 >>> if 1=1: print 'yay!'
...
yay!
 >>> if 1==1: print 'yay!'
Parsing error: file <stdin>, line 1:
if 1==1: print 'yay!'
       ^
Unhandled exception: run-time error: syntax error
 >>>

I don't know why this was changed; possibly because of some of the reasons the 
other posters brought up.

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html




More information about the Python-list mailing list