Pydon'ts

Write beautiful Python code

Buy the book on Leanpub or read the articles here.

  736

A short article with all you need to know about sequence indexing in Python – and a bit more.

  2858

If you need to access the items of an iterable but also keep track of their indices, have you considered using enumerate? Let's talk about another of Python's amazing tools to work with for loops.

  9366

for loops are the bread and butter of imperative programming and Python has some really nice tools to work with them. If you want to traverse several structures in parallel, have you considered using zip?

Structural pattern matching is coming in Python 3.10 and the previous Pydon't explored some interesting use cases for the new match statement. This article explores situations for which match isn't the answer.

Structural pattern matching is coming in Python 3.10 and this article explores how to use it to write Pythonic code, showing the best use cases for the match statement.

Python's comparisons operators can be chained to shorten common comparison expressions. Learn the ins and outs of comparison operator chaining and especially the cases you should avoid, namely those where you chain comparison operators that aren't aligned.

  761

Deep unpacking (or nested unpacking) provides a more powerful way for you to write assignments in your code. Deep unpacking can be used to improve the readability of your code and help protect you against unexpected bugs. Learning about deep unpacking will also be very important in order to make the most out of the structural matching feature that is to be introduced in Python 3.10.

  995

Recursion is a technique that you should have in your programming arsenal, but that doesn't mean you should always use recursion when writing Python code. Sometimes you should convert the recursion to another programming style or come up with a different algorithm altogether.

  2056

All Python objects can be used in expressions that should return a boolean value, like in an if or while statement. Python's built-in objects are usually Falsy (interpreted as False) when they are “empty” or have “no value” and otherwise they are Truthy (interpreted as True). You can define this behaviour explicitly for your own objects if you define the __bool__ dunder method.

  1182

Python's str and repr built-in methods are similar, but not the same. Use str to print nice-looking strings for end users and use repr for debugging purposes. Similarly, in your classes you should implement the __str__ and __repr__ dunder methods with these two use cases in mind.

The walrus operator := can be really helpful, but if you use it in convoluted ways it will make your code worse instead of better. Use := to flatten a sequence of nested ifs or to reuse partial computations.

In Python, if you are doing something that may throw an error, there are many cases in which it is better to "apologise than to ask for permission". This means you should prefer using a try block to catch the error, instead of an if statement to prevent the error.

How should you unpack a list or a tuple into the first element and then the rest? Or into the last element and everything else? Pydon't unpack with slices, prefer starred assignment instead.

The "Zen of Python" is the set of guidelines that show up in your screen if you import this. If you have never read them before, read them now and again from time to time. If you are looking to write Pythonic code, write code that abides by the Zen of Python.

  3575

"Pydon'ts" are short, to-the-point, meaningful Python programming tips. A Pydon't is something you should not do when programming in Python. In general, following a Pydon't will make you write more Pythonic code.