InEffective Python Limericks – Ch 1

  • by

I’ve started reading “Effective Python” (2nd ed) by Brett Slatkin. As a fun exercise and inspired by listening to NPR’s “Wait Wait… Don’t Tell Me” way too late at night, I wrote these limerick summaries for each of the items in the first chapter. If you feel like reading some limericks with wrong tempo and very tortured rhymes about the inner workings of Python, then you’re luck!

Item 1: Know Which Version of Python You’re Using
Progress is important, it’s true.
But it’s hard to keep up with what’s new.
Use 2to3 or six
To get vital bugs fixed
And upgrade from Python 2.

Item 2: Follow the PEP 8 Style Guide
Formatting might feel like an aside,
Why bother with a strict style guide?
We follow PEP 8
To keep our code straight
And grok source code others provide.

Item 3: Know the differences between bytes, str, and unicode
You can’t compare str and bytes
For they’re composed of two different types
Str has unicode,
And bytes an 8-bit load.
Use helpers to get the type right.

Item 4: Write helper functions instead of complex expressions
To avoid complex one-line expressions,
And gain readability with minimal concessions,
Use helper functions,
prefer if/else constructions
and seek options for visual noise suppression.

Item 5: Know How to Slice Sequences
In Python, sequence slicing fulfills a desire
To get multiple items from a provider.
Indices out of bounds
Just wrap around,
And the start and end index aren’t required.

Item 6: Avoid using start, end, and stride in a single slice
When slicing, don’t use a negative stride.
Don’t let all three parameters collide.
If you must disagree
And use all three,
islice from itertools will provide.

Item 7: Use List Comprehensions Instead of map and filter
There’s no lambda in list comprehension
And skipping input items is an easy extension.
Dictionaries and sets
Are also at their best
In supporting comprehension expressions.

Item 8: Avoid more than two expressions in list comprehensions
List comprehensions are tempting to nest,
But limiting the loop levels is best,
Two or fewer is fine,
or if it fits on one line,
but use for loops and indent the rest.

Item 9: Consider Generator Expressions for Large Comprehensions
Large items, generators can weather,
since producing one at a time is better,
unlike a list call,
which builds it all,
and generator expressions can chain together.

Item 10: Prefer enumerate over range
Using range with indexing makes lots of clutter
But it’s enumerate’s bread and butter
A tuple’s returned
With the count concerned,
Conditioned on the initial count setter.

Item 11: Use zip to process iterators in parallel
Multiple iterators can be combined with zip
But careful, the shortest length clips
If you want to the last
With None for those past
Use zip_longest from itertools as a fix.

Item 12: Avoid else blocks after for and while loops
Else blocks after loops are a pain.
Using them only stresses the brain.
They run when no break,
But the effort they take
To follow makes it hard to maintain.

Item 13: Take advantage of each block in try/except/else/finally
Except and else are mutually exclusive
the latter runs if exceptions were elusive
the former runs for fails
but the finally block trails
and executes regardless of conclusion.

Leave a Reply

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