- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are common programming errors or 'gotchas' in Python?
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 −
listLambdas = [lambda x : i + x for i in range(5)] for lam in listLambdas: print(lam(10))
This will give the output −
14 14 14 14 14
Shocked? This is due to binding in closures. 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.
- What are common Python programming mistakes programmers do?
- Are Python Exceptions runtime errors?
- What are common practices for modifying Python modules?
- Common undefined behaviours in C++ programming
- What are environment variables available in Python CGI Programming?
- What are some common funeral etiquette?
- What are undefined reference/unresolved external symbol errors in C++?
- What are Closures in Lua Programming?
- Database Handling Errors in Python
- What are the modules required for CGI programming in Python?
- How are errors classified in trial balance?
- What are the Common Honeypot Traps in Cybersecurity?