RSS Planet Python

Rodrigo Girão Serrão: A generator, duck typing, and a branchless conditional walk into a bar

A friend suggested everyone has a favorite line of code, initially in the context of the APL programming language. This sparked contemplation about a personal favorite line in Python. The author finds generators particularly appealing. Python itself is praised for its beginner-friendliness, vibrant community, and object-oriented nature. Laziness in programming is considered beneficial, leading to efficient solutions. Generators embody this laziness by producing values only when needed, similar to Python's built-in `range` function. Creating a `range` object, regardless of its size, is instantaneous because it doesn't compute the numbers upfront. Printing a `range` object confirms this by displaying its representation, not its contents. This contrasts sharply with creating a list, which requires memory and time to store all elements. Even a small list creation is demonstrated to be quick. However, the performance difference becomes significant for larger datasets.
favicon
mathspp.com
mathspp.com
Image for the article: Rodrigo Girão Serrão: A generator, duck typing, and a branchless conditional walk into a bar
Create attached notes ...