First, note that I understand that ==
is used for comparing two expressions, while =
is used for assigning a value to a variable. However, python is such a clean language with minimal syntax requirements, that this seems like an easy operator to axe. Also I am not trying to start a debate or discussion, but rather learn if there is something that I'm missing to improve my knowledge of programming.
Just as (in python) we don't need to declare a variable to be an int, or a string, and the language determines this based on the value assigned, why doesn't the 'if' statement simply determine that the =
is a comparison, not an assignment?
Another example is that python got rid of many of the {} and [] in other languages and simply used the indentation, because indenting and using braces is redundant. It seems to me that if foo == goo:
is also redundant. But perhaps there's something I'm not yet aware of. Hence the question!

- 87
- 7
-
It's funny because I had just seen this video: [Interview with Tim Peters](http://www.youtube.com/watch?v=1wAOy88WxmY) where he said he started working with Python 0.9.1 where the `=` sign was in fact used for both comparison and assignment – jamylak Apr 03 '13 at 03:34
-
Now I need to watch that one ... I didn't know that was ever the case. Of course, when I started working with python, we were at python2.6... Not a whole lot has changed since then (python3.something was already out ...) – mgilson Apr 03 '13 at 03:45
2 Answers
One very simple reason is that python allows boolean expressions:
a = b == c
and also multiple assignment:
a = b = c
In the first case, a
gets assigned a boolean value* (True
or False
) depending on whether b
and c
are equal. In the second case, a
and b
end up referencing the same object (c
). Clearly you can't support both with only a single operator.
I suppose that you could (in principle) overload =
only within if
statements (since assignment isn't allowed there), but that would get confusing -- Especially for people coming from C
where an assignment is allowed in an if
statement. The zen wins again ("Explicit is better than implicit").
- It doesn't actually have to be a boolean value. It is actually whatever is returned by
a
's__eq__
method (orb
's__eq__
if the former returnsNotImplemented
) -- most objects return a boolean, but a few don't (numpy.ndarray
is one common object which has an__eq__
which returns anotherndarray
for instance).
-
2In that sense, one line containing `b == c` would make a complete statement, and for sure this is different than `b = c`. +1 – heltonbiker Apr 03 '13 at 02:48
-
Thanks. Makes sense. And I agree, you wouldn't want to change it for if statements only. That would go against the overarching principle of keeping python consistent. – user2218093 Apr 03 '13 at 03:01
-
@heltonbiker -- That's a completely valid point, especially from the interactive prompt where `b == c` is the same thing as `print repr(b == c)`. And of course, let's not forget that due to python's dynamic nature, it's hard to know what `b` and `c` are at runtime. One of them could have a really funky `__eq__` with side-effects or whatnot, so the expression `b == c` could (in principle) be "useful" even if you're not at the interactive prompt. – mgilson Apr 03 '13 at 03:01
The two operators can overlap. For instance, consider
a = b = c
which sets a
and b
both to c
, and
a = b == c
which sets a
to either True
or False
based on whether b
and c
are equal.
More generally, Python attempts to avoid syntax that is even possibly ambiguous to allow the parser to be simpler. Even if the ambiguity above could be resolved, it would involve adding a number of special cases, and generally addng complexity to the parser. Keeping the two operators separate neatly avoids the issue.