# What are common Python programming mistakes programmers do?

PythonProgramming

Here are some of the most common python programming mistakes/gotchas that programmers commit −

• Scope name lookups − Python follows scoping rules in order of LEGB(Local, Enclosing, Global, Built-in). Since python has no strict type binding, programmers can reassociate an outer scope variable to another value that might be used in the outer scope later but is now replaced by some other value.

• Not differentiating between is and == − The is an operator in python checks if both objects refer to the same memory address. The == operator executes __eq__ function which might check for equality differently for different classes.

• Modifying a list while iterating over it − If you remove elements from a list while iterating over it, you will get an IndexError. This is because of the list reducing in size while you are not reducing the index with it.

• Closure binding: Consider the following code −

## Example

listLambdas = [lambda x : i + x for i in range(5)]
for lam in listLambdas:
print(lam(10))

## Output

This will give the output:

14
14
14
14
14

Shocked? This is due to binding enclosures. All the lambdas in this list REFER to the variable i, ie, when it changes, these lambdas start referring to the new value.

• Name clash with built-ins − You must have at some point in time created a variable called sum. Note that you reassigned a reference to the sum function in this case. This seems trivial for things like this but can cause some serious issues when packages are named in such a way. This might lead to other packages importing your classes instead of standard ones.

• Unintuitive implementations for operators − Python provides a way to overload operator functions for classes. More often than not, people tend to implement these operators in an uncommon way and end up creating complex and unintuitive APIs.

Published on 19-Apr-2018 16:23:47