I posted a Python tidbit about how for loops can assign to other things than simple variables, and many people were surprised or even concerned:
params = {
"query": QUERY,
"page_size": 100,
}
# Get page=0, page=1, page=2, ...
for params["page"] in itertools.count():
data = requests.get(SEARCH_URL, params).json()
if not data["results"]:
break
...
This code makes successive GET requests to a URL, with a params dict as the data payload. Each request uses the same data, except the “page” item is 0, then 1, 2, and so on. It has the same effect as if we had written it:
for page_num in itertools.count():
params["page"] = page_num
data = requests.get(SEARCH_URL, params).json()
One reply asked if there was a new params dict in each iteration. No, loops in Python do not create a scope, and never make new variables. The loop target is assigned to exactly as if it were an assignment statement.
As a Python Discord helper once described it,
While loops are “if” on repeat. For loops are assignment on repeat.
A loop like for <ANYTHING> in <ITER>:
will take successive
values from <ITER>
and do an assignment exactly as this statement
would: <ANYTHING> = <VAL>
. If the assignment statement is
ok, then the for loop is ok.
We’re used to seeing for loops that do more than a simple assignment:
for i, thing in enumerate(things):
...
for x, y, z in zip(xs, ys, zs):
...
These work because Python can assign to a number of variables at once:
i, thing = 0, "hello"
x, y, z = 1, 2, 3
Assigning to a dict key (or an attribute, or a property setter, and so on) in a for loop is an example of Python having a few independent mechanisms that combine in uniform ways. We aren’t used to seeing exotic combinations, but you can reason through how they would behave, and you would be right.
You can assign to a dict key in an assignment statement, so you can assign to it in a for loop. You might decide it’s too unusual to use, but it is possible and it works.
Comments
I think it is a terrible idea. It is not a standard Python idiom and the intent of the code is not obvious without reflection, and good code should not require reflection. Just because you CAN do something doesn’t mean you should.
I’ve written my share of APL code and have seen the end of the slippery slope. It is not pretty.
no
I think this is a cool idea. There is an argument that if it’s cool it’s Pythonic. The intent was immediately clear without any reflection other than to acknowledge how cool it is. If it’s quicker, shorter, more readable and reliable I can’t think of any reason not to do it.
I’ve written more than my fair share of Perl and thank God that Python isn’t Perl any more than Python is APL.
Thank you for the post. I may never use this directly, but I have a deeper understanding of for-loops now.
I’m surprised by the opposition to this code snippet. Why not utilize the assignment in the for loop to stash the value where you need it? I do agree that the version with page_num is more readable, so that’s good. But it also adds an extra variable and one more line of code. In this case the code is small enough that this won’t matter much. But the extra code does add extra opportunity for errors and bugs to creep in during maintenance and refactoring. I’d say both versions are fine, and are coder preference.
I like that this is available. Just the other day I needed to add an enumeration in an existing loop and this assignment is perfect.
Add a comment: