Python Anti-Patterns

“Python Anti-Patterns” delves into common pitfalls and mistakes encountered in Python programming, offering insights on how to avoid them. By drawing from a variety of Python-related sources, the document sheds light on inefficient coding practices, suboptimal design choices, and misconceptions that can hinder the effectiveness and readability of Python code. Through examples and explanations, it aims to guide developers towards writing more efficient, maintainable, and Pythonic code by highlighting what not to do. The document serves as a valuable resource for programmers looking to enhance their Python skills and avoid common pitfalls in their coding practices.

Using

 Python Anti-Patterns

The function isinstance is the best-equipped to handle type checking because it supports inheritance (e.g. an instance of a derived class is an instance of a base class, too). Therefore isinstance sh...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Not using

 Python Anti-Patterns

PEP 20 states “There should be one– and preferably only one –obvious way to do it.” The preferred way to iterate over the key-value pairs of a dictionary is to declare two variables in a for loop, an...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Using

 Python Anti-Patterns

Per the PEP 8 Style Guide, function names should be lowercase, with words separated by underscores. Anti-pattern Best practice Using lowercase with underscores The code below uses the PEP 8 preferred...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Asking for permission instead of forgiveness

 Python Anti-Patterns

The Python community uses an EAFP (easier to ask for forgiveness than permission) coding style. This coding style assumes that needed variables, files, etc. exist. Any problems are caught as exceptio...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Not using

 Python Anti-Patterns

PEP 20 states “There should be one– and preferably only one –obvious way to do it.” The preferred way to iterate through a pair of lists is to declare two variables in a loop expression, and then cal...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Comparing things to

 Python Anti-Patterns

Per the PEP 8 Style Guide, the preferred ways to compare something to True are the patterns if cond is True: or if cond: . This is only a guideline. It can be ignored if needed. But the purpose of th...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Not using named tuples when returning more than one value from a function

 Python Anti-Patterns

Named tuples can be used anywhere where normal tuples are acceptable, but their values can be accessed through their names in addition to their indexes. This makes the code more verbose and readable....

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Using

 Python Anti-Patterns

For simple transformations that can be expressed as a list comprehension, use list comprehensions over map() or filter() . Use map() or filter() for expressions that are too long or complicated to ex...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Putting type information in a variable name

 Python Anti-Patterns

Python is a duck-typed language. Just because a variable is described as an integer does not mean that it actually is an integer. This can be very dangerous for any programmer who acts on the variabl...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Not using dict keys when formatting strings

 Python Anti-Patterns

When formatting a string with values from a dictionary, you can use the dictionary keys instead of explicity defining all of the format parameters. Consider this dictionary that stores the name and a...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Comparing things to

 Python Anti-Patterns

Per the PEP 8 Style Guide, the preferred way to compare something to None is the pattern if Cond is None . This is only a guideline. It can be ignored if needed. But the purpose of the PEP 8 style gu...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents

Not using dict comprehensions

 Python Anti-Patterns

You may encounter the old style of initializing a dict (passing an iterable of key-value pairs) in older Python code written before version 2.7. The new dict comprehension style is functionally equiv...

📚 Read more at Python Anti-Patterns
🔎 Find similar documents