A generator, duck typing, and a branchless conditional walk into a bar
Briefly

A generator, duck typing, and a branchless conditional walk into a bar
"My friend Aaron is quite a character. One day, he was giving a talk and said that everyone must have their favourite line of code. And he was absolutely sure of what he was saying. His conviction is that everyone is so deep into their craft that they naturally feel strongly about some lines of code. So much so that one of them is their personal favourite."
"One other thing I like in general, and that translates well into the world of Python, is laziness. A pinch of laziness in a programmer is a good thing because it will force you to think about the best way of doing things, and Python has a category of objects that are inherently lazy, which are generators."
"If you run range(10), that small expression runs in a tiny fraction of a second because it just instantiates an object called "range" and doesn't really compute the integers from 0 to 9, inclusive. You can see that if you print the object:"
A conviction about programmers having favourite lines of code connects to terse, expressive traditions in other languages. Python supports laziness through generators, which are iterable objects that produce values only when requested. Laziness encourages efficient thinking by avoiding unnecessary computation and memory use. The built-in range exemplifies this: instantiating range does not compute all integers up front, so creating even a very large range is fast. Printing a range shows the object rather than its elements, and iteration triggers generation of values as needed.
Read at Mathspp
Unable to calculate read time
[
|
]