Friday, 18 October 2013

Comprehending comprehensions.

List comprehensions and generator objects seem to me to be one of the niftiest things we're covering for Python. Seriously, you get to consolidate maybe four or five lines of code down into one simple line. On top of that, the line is almost identical to an English sentence! Seriously, isn't that cheating? It feels like cheating.

What I honestly would like to know is exactly how they work, because whenever the IDLE debugger steps through it, I just see the object reference, and the MAGIC happen. Comprehensions are actually kind of a pain when debugging, because I get to slowly watch nothing I can comprehend happen. The comprehensions sometimes seen incomprehensible! I find it's a lot cleaner to write out code the long way first, and then when you get it working, thusly consolidate it into a comprehension afterwards.

No comments:

Post a Comment