What is the purpose of the '==' operator when comparing values vs '='?

python

Solution

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 (or `b`'s `__eq__` if the former returns `NotImplemented`) -- most objects return a boolean, but a few don't (`numpy.ndarray` is one common object which has an `__eq__` which returns another `ndarray` for instance).

Problem

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!

Original source

Related problems