I came across an expression like:
|
|
I had no idea one could use expressions like self.batch
as the loop iterator.
This works as expected, and right after the last iteration of the loop (assuming
there was one), self.batch
is set to the last item of batches
. It also
works if self
had no attribute called batch
initially.
So I was curious and looked up the relevant portion of the grammar, which is:
|
|
So the left side of the for .. in ..
construct can contain “star_targets
”:
|
|
This is a bit more flexible than being able to use an attribute for the iterator.
- Using an attribute:
|
|
- Using a
*sequence
.
|
|
In this form, very sensibly, you can have at most one *starred
, and specifying
more than one is a syntax error.
- Using a slice
|
|
I think I’ve needed to do what each of the above are doing, but I have always gone about it in a more direct style. I am not sure if these lesser known ways of iteration are immediately more or less readable, but they don’t look too clunky.