3 ms·
> Assignment is not an expression. We've written 20K of production Kotlin code without this ever being a useful convention. Assignment being an expresssion is l
by pyre 10y ago
> Assignment is not an expression. We've written 20K of production Kotlin code without this ever being a useful convention. Assignment being an expresssion is layover from the C family of languages. If you really want C's syntax and semantics, go write C.
Python doesn't treat assignment as an expression. E.g. I can't do:
if a = 1:
pass
or
def c():
return a = 1
or:
a = lambda c: c.x = 1
But I can still do:
a = b = c = 2
to set multiple variables at the same time.
- simias 10y agoThat looks rather inconsistent to me. Isn't the last expression equivalent to a = (b = (c = 2)), and if not why?
- scardine 10y agoI guess it is syntax sugar for: c = 2 b = c a = b
- simias 10y agoWhat warrants this special-case sugar though? Is it somehow super convenient in python? I mostly write C code and while it features these chained affectations (as a side effect of assignations being expressions) I never use them. I also seldom encounter them in the wild. Seems odd to me that a "lean and clean" language like python would bother special casing it.
- uryga 10y agoPython has a bunch of special cases like this that make the code more readable (according to it's creators): `x not in xs` --> `not (x in xs)` `0 < x < 10` --> `0 < x and x < 10` `x = y = 5` --> (expanded above) `in` is used both in `for...in...:` and `x in xs` I feel like Python will gladly sacrifice being 'small and lean' if it leads to gains in readability, although they don't introduce these willy-nilly. Obviously, a lot of it is subjective, but I happen to mostly like it.
- noobermin 10y agoYou may never use them but it is used often enough that they included it as syntactic sugar.
- acoard 10y agoYou're overcomplicating it, think of it from a math perspective. Don't just look at it as the "assignment operator", look at it as an equals sign. a=b=c=2. They're all equal, all variables here equal 2.
- simias 10y agoI don't get what maths have to do with it. In maths a = b = 2 is the same as a = (b = 2), (a = b) = 2, (a = b = 2) or a = 2 = b. Clearly it doesn't apply here.
- kupiakos 10y agoIs surrounding an equality with parentheses in math something that is regularly done? When I see (a = b) in math, I don't think of it as "evaluating" to anything like 2+3 would.
- euyyn 10y agoIn math notation I would expect a = (b = c) to mean "a" is a boolean whose value is whether b equals c.
- pyre 10y agoFrom a "math perspective," the equals sign is a statement, not an expression. It is asserting the state of something. In programming, we're not 'stating' that `a = b = c = 2`, we are performing an operation to make it happen. This is why in some early languages the assignment operator was `:=` and the comparison operator was `=`.
- scardine 10y agoThe comparison operators are also interesting: if not 10 < x < 20: raise ValueError("Out of range.") Not every language allow this.