[Python-ideas] Combining test and assignment

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Sun Jan 22 07:23:24 CET 2012

On Jan 21, 2012, at 6:51 PM, Steven D'Aprano wrote:

> I dislike assignment as an expression, and am glad that Python doesn't have it. Even though the form "expr as name" avoids the common C gotcha, I am -1 on adding it to Python.

I find it interesting that Go-lang follows your intuition and so added the "assignment first" conditional. Here's an example from their docs:

if x := f(); x < y {
	return x
} else if x > z {
	return z
} else {
	return y

In Python, we'd just put the x = f() first, but in Go, putting the assignment into the conditional affects the scoping of things. The x goes out of scope once the if is over.

More information about the Python-ideas mailing list