Is it legal to do
a = b = 3
in python? If so, is it a bad practice?
Is it legal to do
a = b = 3
in python? If so, is it a bad practice?
Yes, it is legal to do so. No, it is not bad practice.
Just take into account that the right-hand side, the value expression, is evaluated first, and assignment then takes place from left to right; 3 is assigned to a first, then to b.
From the assignment statement documentation:
An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right.
You assign the same value to all targets. That means that each variable refers to one value only. This is important when that value is mutable, like a list or a dictionary.
if something: true_statement on one line, or using ; to put multiple statements on one line.