Forum Archive

The Little Book of Python Anti-Patterns

ccc

A nice online resource of Python worst practices http://docs.quantifiedcode.com/python-anti-patterns They also have a free automated tool that you can point at any public GitHub repo to find where code might be suboptimal.

A podcast with the authors http://www.talkpythontome.com/episodes/show/18/python-anti-patterns-and-other-mistakes

Yet another example of Python innovation coming from Berlin ;-)

dgelessus

I somehow doubt the knowledge of whoever wrote the first page... Most of the suggestions were alright, but a few things were just wrong:

  • For if conditions it recommends using if cond is True or if cond is False, which is just as wrong as if cond == True and if cond == False that it recommends against.
  • It also mentions if cond and if not cond, but says that those only work if cond is a boolean object, i. e. True or False, which is not true at all - this version works with any object, because all objects have a truth value (usually True), unlike the is test, which only works with True and False exactly, not with other objects with a truth value.
  • It pretends that is and is not behave like isinstance and talks about writing if rectangle is Circle, which is wrong. This misunderstanding might explain the misconception about if statement conditions.
ccc

I agree with your comments but the pages are still a great resource along the lines of the Jeff Knupp's book Writing Idiomatic Python.

I avoid using is altogether because of the blog post Why you should almost never use is in Python. I use if cond and if not cond almost everywhere for all data types because I find it more readable and less error prone. I therefore try to avoid comparing variables directly to True, False, None, '', [], {}, etc in alignment with Jeff Knupp's 5.1.4 Avoid comparing directly to True, False, or None.

cneumann81

Thanks for pointing this out. It's fixed. Feel free to put any other things you might find into our issue tracker or just open a pull request. Any input is highly appreciated.