473,388 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Why = = (and not just =)

Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?
Jul 18 '05 #1
16 1611
Joe Green wrote:
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


Certainly, one could have use "=" to denote the test for equality.
However, I believe that this syntax was inherited from ABC, which
probably inherited it from C. In addition, it *is* useful to
syntactically distinguish between assignments and equality tests
even if the parser does not need that distinction: The human reader
might be tricked into seeing something that isn't there.

Regards,
Martin

Jul 18 '05 #2
Joe Green wrote:
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


I don't particularly like it, but you can chain assignments:
b = 1
a = b = 2
a,b (2, 2)
a = b == 2
a,b (True, 2)


HTH,
Peter
Jul 18 '05 #3
>>>>> "Joe Green" <so*****@microsoft.com> (JG) wrote:

JG> Sorry, I cant help aking stupid questions:
JG> I understand why we need = = in C, but why in Python (or Java),
JG> surely if you write
JG> if a=b: pass # syntax error
JG> it could not possibly mean anything other than what I intended
JG> (which was of course if a = = b:) ?

So what about a = b = c vs. a = b==c?

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #4
Joe Green wrote:
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


Well, it's quite conceivable it might mean != or <= -- as typos:
they're perfectly POSSIBLE, though not quite as LIKELY as the
typo you specifically made. One of Python's principles is: in
fact of ambiguity, resist the temptation to guess.

In PL/I, I remember, I could write...:

if if = then then then = else else else = if

the role of each word and punctuation was perfectly clear to
_the PL/I parser_ -- one '=' is equality, the other two are
assignment, one each of 'if' 'then' and 'else' are keywords
and the others are variable names...

Unfortunately humans aren't quite as smart, so allowing
ambiguous-to-humans constructs because they "could not
possibly mean anything other than" (whatever) turned out
to be terribly error-prone.

I believe just about all languages designed after PL/I
took stock of the lesson (pity that still today some
popular languages have roots in ones designed _before_
PL/I, such as Basic -- they may perpetuate some, though
probably not all, of these "ambiguity" mistakes).

In Python there are other excellent reasons why == is
needed to indicate equality testing in some cases, and
therefore it should be always used (rather than letting
you use = when context allegedly makes it unambiguous)
to avoid making Python a complicated, context-dependent
language. For example,

foo(fee, fie=fum)

passes a keyword argument fie with the value fum, while

foo(fee, fie==fum)

pasees a second positional argument, a bool, which is
True if fie equals fum, otherwise False.
Alex

Jul 18 '05 #5

"Joe Green" <so*****@microsoft.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?
Part of the reason is that Guido very seldom burns his bridges,
even after he's crossed them. Comparison and assignment are
different operators, and they deserve a different operator symbol.
Fortran started the trend of using the same symbol, but Fortran
was unrepentently statement oriented, and had a rather restricted
expression syntax to boot. The original language in the Algol family,
Algol 60, used ":=" as the assignment operator as I recall, a tradition
followed by Pascal.

Frankly, I'd rather have the assignment operator be something
like a left arrow: "<-" perhaps. I think it makes more sense,
and it avoids the confusion between one = and two.

It also allows the developers to add an assignment in expressions
sometime. Not that I think there's much hope of that, but it does
keep the options open.

John Roth

Jul 18 '05 #6
"Joe Green" <so*****@microsoft.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


Well, it could mean "if the value of a is True, after being assigned the
value of b" (which is what it would mean in C/C++, Java, and several other
languages). So, I suppose "==" is used to distinguish between assignment(or
binding) and tests for equality. I suppose the question you could ask
yourself as a language designer is "Is it preferrable to require different
notation for these different operations, or to use the same notation and
have its semantics differ depending upon the context in which it appears?".
In Python, they've chosen the former. I think there are languages that use
the latter, but none come to mind at the moment. Since Python does not allow
assignment in expressions, it's seems reasonable that your suggestion could
be introduced, but I hazard to guess that it would introduce more confusion
than clarity. For instance, people familiar with C/C++, Java, etc. (and even
many who are not familiar with those languages) will have very different
expectations of what "if a = b:" should mean. In fact, there are frequent
requests on this newsgroup for that expression to take on the meaning it has
in those other languages, rather than result in an exception, as it does in
this one (see
http://www.python.org/doc/faq/genera...an-expression).
In an alternate version of the faq (located here:
http://python.is.co.za/doc/faq/gener...an-expression),
there is mention of introducing ":=" for assignment. If you had that, then
you could use "=" as a test for equality with a bit less confusion. Of
course, everyone would have to go back through their old code and change
their "a=b"s to "a:=b", and all of their "a==b"s to "a=b". Hmm. And I
suppose you might want to change "+=", "-=", etc. to "+:=", "-:=", etc. for
symmetry ... on second thought ... maybe not. Anyway, after all of that, the
answer to your question "Why do we need == in Python?" is "We don't, really,
but that's what Guido wanted/chose". At least, I don't believe we _need_
it - I think it would be possible to design the language to interpret "a=b"
as a test for equality, when found in an expression, and as assignment, when
it is not. But they haven't done that in Python, and it doesn't seem likely
that it will be done. I don't have a problem with that. I like the way it's
done now. But, of course, tastes will vary.

Was that at all helpful?
Sean
Jul 18 '05 #7

"Joe Green" <so*****@microsoft.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


It could also mean that the writer was trying to do assignment in an
expression, as one can do in C.

TJR
Jul 18 '05 #8
> expression syntax to boot. The original language in the Algol family,
Algol 60, used ":=" as the assignment operator as I recall, a tradition
followed by Pascal.

Frankly, I'd rather have the assignment operator be something
like a left arrow: "<-" perhaps. I think it makes more sense,
and it avoids the confusion between one = and two.


actually Algol was meant to have the <- rather than the := but due to
problems writting the lexer/parser, it became :=

if you want a language which uses the <- operator then have a look at the
language "Beta" it takes a little getting used to, when used nested ;)
-carlo van dango
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 18 '05 #9
John Roth fed this fish to the penguins on Sunday 19 October 2003 14:23
pm:
Fortran started the trend of using the same symbol, but Fortran
was unrepentently statement oriented, and had a rather restricted
Pardon? There is a great difference between = and .eq. in FORTRAN. F90
is the first standard Fortran I've seen that permitted symbols (=, <,) in place of the dotted notation (.eq., .lt., .gt.)
Now, I'll be the first to admit that the syntax does have some rather
nasty look-ahead cases...

do 10 i = 2 . 9 3
vs
do 10 i = 2 , 9, 3

wherein the first is a floating point assignment to a variable named
"do10i", and the second as a loop to statement-label 10 with index "i"
-- and you can't parse the difference until you hit the "." or ","!
expression syntax to boot. The original language in the Algol family,
Algol 60, used ":=" as the assignment operator as I recall, a
tradition followed by Pascal.
And thereby transported into Ada (c.l.a currently has a flame going
from someone insisting Ada must include C-style assignments -- +=, etc.
-- to be viable... and some of the notation is now varying between +=
and :+ )
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #10

"Dennis Lee Bieber" <wl*****@ix.netcom.com> wrote in message
news:fn************@beastie.ix.netcom.com...
John Roth fed this fish to the penguins on Sunday 19 October 2003 14:23
pm:
Fortran started the trend of using the same symbol, but Fortran
was unrepentently statement oriented, and had a rather restricted
Pardon? There is a great difference between = and .eq. in FORTRAN.

F90 is the first standard Fortran I've seen that permitted symbols (=, <,
) in place of the dotted notation (.eq., .lt., .gt.)


But the dotted symbols came later. They weren't in the original Fortran's
in the 50s and 60s that I learned from...

The original Fortran was a syntactic mess - a large part of the impetus
for all the work on computer language syntax was caused by the
horrors of Fortran and COBOL syntax.

John Roth
Jul 18 '05 #11
"John Roth" <ne********@jhrothjr.com> wrote in message news:<vp************@news.supernews.com>...
Fortran started the trend of using the same symbol, but Fortran
was unrepentently statement oriented, and had a rather restricted
expression syntax to boot. The original language in the Algol family,
Algol 60, used ":=" as the assignment operator as I recall, a tradition
followed by Pascal.

You must be thinking of BASIC.
Fortran did no such thing. Originally, Fortran had only the arithmetic if, i.e.,
IF (A-B) 10, 20, 30

Later, the logical if was added,
IF (A .EQ. B) GO 20
Jul 18 '05 #12
John Roth fed this fish to the penguins on Sunday 19 October 2003 17:22
pm:

But the dotted symbols came later. They weren't in the original
Fortran's in the 50s and 60s that I learned from...
Hmmm, they existed in FORTRAN IV (1966 standard). I'll have to concede
that what F-II had I wouldn't know...
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #13
Joe Green wrote:
Sorry, I cant help aking stupid questions:

I understand why we need = = in C, but why in Python (or Java),
surely if you write
if a=b: pass # syntax error
it could not possibly mean anything other than what I intended
(which was of course if a = = b:) ?


But it leads to ambiguities in other situations... a = b = c, for example.

Interestingly, very old versions of Python did use = as the equality operator.
See Misc/HISTORY in the source distribution:

New features in 0.9.6:
[...]
- '==' is now the only equality operator; "../demo/scripts/eqfix.py" is
a script that fixes old Python modules

Release 0.9.3 (never made available outside CWI)
[...]
- C comparison operators: == != (the old = and <> remain valid).

ABC apparently used = as well, and <> for inequality. I'm assuming this is
where the first versions of Python got it from. Judging from the history file,
= was soon replaced by ==; <> is still valid today, but deprecated in favor of !=.

Cheers,

--
Hans (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #14
"John Roth" <ne********@jhrothjr.com> wrote in
news:vp************@news.supernews.com:
Frankly, I'd rather have the assignment operator be something
like a left arrow: "<-" perhaps. I think it makes more sense,
and it avoids the confusion between one = and two.


This would be just as ambiguous as making both assignment and comparison
use '='. Compare:

a<-b<-c

a=b=c
--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Jul 18 '05 #15

"Duncan Booth" <du****@NOSPAMrcp.co.uk> wrote in message
news:Xn***************************@127.0.0.1...
"John Roth" <ne********@jhrothjr.com> wrote in
news:vp************@news.supernews.com:
Frankly, I'd rather have the assignment operator be something
like a left arrow: "<-" perhaps. I think it makes more sense,
and it avoids the confusion between one = and two.
This would be just as ambiguous as making both assignment and comparison
use '='. Compare:

a<-b<-c

a=b=c


1. I didn't suggest using the same symbol for both.

2. I'm quite well aware of the syntactic issue in Python. Unfortunately,
there
aren't any good solutions without adding extra symbols that aren't on the
keyboard.

John Roth

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

Jul 18 '05 #16
On Mon, 20 Oct 2003 00:59:30 +0200, Carlo v. Dango wrote:
expression syntax to boot. The original language in the Algol family,
Algol 60, used ":=" as the assignment operator as I recall, a tradition
followed by Pascal.

Frankly, I'd rather have the assignment operator be something
like a left arrow: "<-" perhaps. I think it makes more sense,
and it avoids the confusion between one = and two.


actually Algol was meant to have the <- rather than the := but due to
problems writting the lexer/parser, it became :=

if you want a language which uses the <- operator then have a look at the
language "Beta" it takes a little getting used to, when used nested ;)


Actually, if I recall correctly, BETA <URL:http://www.daimi.au.dk/~beta/>
uses the "->" operator--assignment goes the other way.

In BETA there's syntactical difference between assignment and equality
tests (= for testing equality, -> for assigning), but assignment and
method calls uses the same syntax. So assigning to variable x, and
calling function f, uses the same operator:

5 -> x; 5 -> f;
Just nitpicking...

/mailund

Jul 18 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Elaine Jackson | last post by:
I'm new to Python, and I've noticed the following: >>> def f(a,b): a+=b >>> def g(a,b): a=a+b >>> p= >>> q= >>> r= >>> s=
8
by: Tom Siltwater | last post by:
building variable width/DB tables etc using getrows instead of movenext. Performance is a major concern as this app requires SSL. My question is, when does it become more about the challenge of...
11
by: Steven Burn | last post by:
Just wondering if it would be possible to convert an existing e-mail form to; 1. Send the e-mail (as it does now...without problems) 2. Copy one folder on the server, to another new folder ...
2
by: Börni | last post by:
Hi, I'm just wondering why one cant do something like this outside of an function: alert(document.getElementById("edit")); this just gives null, but inside a function it works.
1
by: Martin Russ | last post by:
Hi, Playing around with Atlas (March CTP). I have a block of tables followed by a timercontrol that is supposed to update an UpdatePanel for the date/time. The timercontrol is working great,...
2
by: cindy | last post by:
i build a string string eProduct = "#blah#ablah#bbbb"; StringBuilder sb = new StringBuilder(eProduct); eProduct = sb.Replace("#","").ToString(); string aProducts= eProduct.Split(';'); then I...
19
by: John Salerno | last post by:
Sorry to post here about this again, but the hint forums are dead, and the hints that are already there are absolutely no help (mostly it's just people saying they are stuck, then responding to...
22
by: Ark Khasin | last post by:
Due to a peculiar need (code instrumentation) I came across unexpected behavior of Visual Studio 6.0 and 2005 (doing the same thing): #include <stdio.h> #define CAT1(a,b) a ## b #define...
0
by: manikandan | last post by:
dont miss it just open dont miss it just open dont miss it just open #############################
5
by: John Salerno | last post by:
I just installed Pylons onto my hosting server so I could try out templating with Mako, but it seems a little more complicated than that. From the look of it all, the site seems to want a full...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.