Python Anti-Patterns

7 pointsposted a month ago
by fanf2

5 Comments

zephen

a month ago

Most of the rules in that list are certainly things you would be better off following than not.

But some of them aren't, and many of the ones that are valid are simple programming errors that will be caught as soon as you run any data through your program. Which would seem to stretch the term anti-pattern past its breaking point.

This list reminds me of a letter to the editor I saw, maybe 40 years ago or so, in Dr. Dobb's.

This is (partially) paraphrased, but not by much, because the letter was short and sweet:

Dear sirs:

Everything in your magazine is either obvious or wrong.

Please cancel my subscription.

Sincerely, xxxxx

gnabgib

a month ago

(2018 / 2015) Title: The Little Book of Python Anti-Patterns

0xFF0123

a month ago

Curious which of these are not covered by ruff

zahlman

a month ago

Almost all of them are covered by just having some experience in the language and a minimum of good taste; and many are covered by Python itself.

Also, a bunch of things are being bundled together as "anti-patterns" here that just don't match the traditional idea of what I always saw written up that way. A lot of it is just "failure to use the usual pattern".

zephen

a month ago

And a lot of the rationale is very weak tea.

For example, don't assign a lambda to an identifier, because a function will show up in a stack trace. But it's OK to use the lambda directly. (huh. What happened to the necessity for the stack trace?)

(FWIW, I think that in some cases assigning lambdas to identifiers is peachy-keen fine. Visually, I immediately know I'm looking at something that doesn't itself produce any side-effects.)

"Using single letter variable names." Uh, yeah, when to do this isn't Python-specific, and includes some cases when it's actually better.

"Not using defaultdict()" Sure, sure, having keys automagically populate is goodness. But then, "Dynamically creating variable/method/function names" is an anti-pattern. Schizophrenic much?

And then, we have the outright lies, such as:

"When a method is not preceded by the @staticmethod or @classmethod decorators and does not contain any references to the class or instance (via keywords like cls or self), Python raises the Method could be a function error. This is not a critical error, but you should check the code in question in order to determine if this section of code really needs to be defined as a method of this class."

Nobody should take advice from a third-grader who can't discern the difference between the interpreter and his preferred lint-like substance.