3 ms·
I guess it is syntax sugar for: c = 2 b = c a = b
by scardine 10y ago
I 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.