I'll explain with an example.

Let's say that you've got some existing nested for-loop that you want to inline:

for bytestring in x:
	for i in bytestring:
		for k in reversed(range(8)):
			yield ((i & (0b1 << k)) >> k)

Intuitively, you might think that you would type something like "bit for k in bits for bits in in bytes for bytes in x" — but this is wrong.

Instead, you must leave the fors in the same order as in the code-block form, only popping the inner expression itself to the front:

iter(  ((i & (0b1 << k)) >> k) for bytestring in x for i in bytestring for k in reversed(range(8))  )

Leave a Reply

Your email address will not be published. Required fields are marked *

Warning: This site uses Akismet to filter spam. Until or unless I can find a suitable replacement anti-spam solution, this means that (per their indemnification document) all commenters' IP addresses will be sent to Automattic, Inc., who may choose to share such with 3rd parties.
If this is unacceptable to you, I highly recommend using an anonymous proxy or public Wi-Fi connection when commenting.