
8 Common Python Mistakes You Shall Avoid
Introduction
Python is a very powerful programming language with easily understandable syntax which allows you to learn by yourself even you are not coming from a computer science background. Through out the learning journey, you may still make lots mistakes due to the lack of understanding on certain concepts. Learning how to fix these mistakes will further enhance your understanding on the fundamentals as well as the programming skills.
In this article, I will be summarizing a few common Python mistakes that many people may have encountered when they started the learning journey and how they can be fixed or avoided.
Reload Modules after Modification
Have you ever wasted hours to debug and fix an issue and eventually realized you were not debugging on your modified source code? This usually happens to the beginners as they did not realize the entire module was only loaded into memory once when import statement was executed. So if you are modifying some code in separate module and import to your current code, you will have to reload the module to reflect the latest changes.
To reload a module, you can use the reload function from the importlib module:
from importlib import reload # some module which you have made changes import externallib reload(externallib)
Naming Conflict for Global and Local Variables
Imagine you have defined a global variable named app_config, and you would like to use it inside the init_config function as per below:
app_config = "app.ini" def init_config(): app_config = app_config or "default.ini" print(app_config)
You may expect to print out “app.ini” since it’s already defined globally, but surprisedly you would get the “UnboundLocalError” exception due to the variable app_config is referenced before assignment. If you comment out the assignment statement and just print out the variable, you would see the value printed out correctly. So what is going on here?
The above exception is due to Python tries to create a variable in local scope whenever there is an assignment expression, and since the local variable and global variable have the same name, the global variable being shadowed in local scope. Thus Python throws an error saying your local variable app_config is used before it’s initialized.
To solve this naming conflict, you shall use different name for your global variable and local variables to avoid any confusion, e.g.:
app_config = "app.ini" def init_config(): config = app_config or "default.ini" print(config)
Checking Falsy Values
Examining true or false of a variable in if or while statement sometimes can also go wrong. It’s common for Python beginners to mix None value and other falsy values and eventually write some buggy code. E.g.: assuming you want to check when price is not None and below 5, trigger some selling alert:
def selling_alert(price): if price and price < 5: print("selling signal!!!")
Everything looks fine, but when you test with price = 0, you would not get any alert:
selling_alert(0) # Nothing has been printed out
This is due to both None and 0 are evaluated as False by Python, so the printing statement would be skipped although price < 5 is true.
In python, empty sequence objects such as “” (empty string), list, set, dict, tuple etc are all evaluated as False, and also zero in any numeric format like 0 and 0.0. So to avoid such issue, you shall be very clear whether your logic need to differentiate the None and other False values and then split the logic if necessary, e.g.:
if price is None: print("invalid input") elif price < 5: print("selling signal!!!")
Default Value and Variable Binding
Default value can be used when you want to make your function parameter optional but still flexible to change. Imagine you need to implement a logging function with an event_time parameter, which you would like to give a default value as current timestamp when it is not given. You can happily write some code as per below:
from datetime import datetime def log_event_time(event, event_time=datetime.now()): print(f"log this event - {event} at {event_time}")
And you would expect as long as the event_time is not provided during log_event_time function call, it shall log an event with the timestamp when the function is invoked. But if you test it with below:
log_event_time("check-in") # log this event - check-in at 2021-02-21 14:00:56.046938 log_event_time("check-out") # log this event - check-out at 2021-02-21 14:00:56.046938
You shall see that all the events were logged with same timestamp. So why the default value for event_time did not work?
To answer this question, you shall know the variable binding happens during the function definition time. For the above example, the default value of the event_time was assigned when the function is initially defined. And the same value will be used each time when the function is called.
To fix the issue, you can assign a None as default value and check to overwrite the event_time inside your function call when it is None. For instance:
def log_event_time(event, event_time=None): event_time = event_time or datetime.now() print(f"log this event - {event} at {event_time}")
Similar variable binding mistakes can happens when you implement your lambda functions. For your further reading, you may check my previous post why your lambda function does not work for more examples.
Default Value for Mutable Objects
Another mistake Python beginners trend to make is to set a default value for a mutable function parameter. For instance, the below user_list parameter in the add_white_list function:
def add_white_list(user, user_list=[]): user_list.append(user) return user_list
You may expect when user_list is not given, a empty list will be created and then new user will be added into this list and return back. It is working as expected for below:
my_list = add_white_list('Jack') # ['Jack'] my_list = add_white_list('Jill', my_list) #['Jack', 'Jill']
But when you want to start with a empty list again, you would see some unexpected result:
my_new_list = add_white_list('Joe') # ['Jack', 'Jill', 'Joe']
From the previous variable binding example, we know that the default value for user_list is created only once at the function definition time. And since list is mutable, the changes made to the list object will be referred by the subsequent function calls.
To solve this problem, we shall give None as the default value for user_list and use a local variable to create a new list when user_list is not given during the call. e.g.:
def add_white_list(user, user_list=None): if user_list is None: user_list = [] user_list.append(user) return user_list
Someone may get confused that datetime.now() shall create a Python class instance, which supposed to be mutable also. If you checked Python documentation, you would see the implementation of datetime is actually immutable.
Misunderstanding of Python Built-in Functions
Python has a lot of powerful built-in functions and some of them look similar by names, and if you do not spend some time to read through the documentation, you may end up using them in the wrong way.
For instance, you know built-in sorted function or list sort function both can be used to sort sequence object. But occasionally, you may make below mistake:
random_ints = [80, 53, 7, 92, 30, 31, 42, 10, 42, 18] # The sorting is done in-place, and function returns None even_integers_first = random_ints.sort(key=lambda x: x%2) # Sorting is not done in-place, function returns a new list sorted(random_ints)
Similarly for reverse and reversed function:
# The reversing is done in-place, and function returns None random_ints = random_ints.reverse() # reversing is not done in-place, function returns a new generator reversed(random_ints)
And for list append and extend function:
crypto = ["BTC", "ETH"] # the new list will be added as 1 element to crypto list crypto.append(["XRP", "BNB"]) print(crypto) #['BTC', 'ETH', ['XRP', 'BNB']] # the new list will be flattened when adding to crypto list crypto.extend(["UNI"]) print(crypto) # ['BTC', 'ETH', ['XRP', 'BNB'], 'UNI']
Modifying Elements While Iterating
When iterating a sequence object, you may want to filter out some elements based on certain conditions.
For instance, if you want to iterate below list of integers and remove any elements if it is below 5. You probably would write the below code:
a = [1, 2, 3, 4, 5, 6, 2] for b in a: if b < 5: a.remove(b)
But when checking the output of the list a, you would see the result is not as per you expected:
print(a) # [4, 5, 6, 2]
This is because the for statement will evaluate the expression and create a generator for iterating the elements. Since we are deleting elements from the original list, it will also change the state of the generator, and then further cause the unexpected result. To fix this issue, you can make use of the list comprehension as per below if your filter condition is not complex:
[b for b in a if b >= 5]
Or if you wish, you can use the the filterfalse together with the lambda function:
from itertools import filterfalse list(filterfalse(lambda x: x < 5, a))
Re-iterate An Exhausted Generator
Many Python learners started writing code without understanding the difference between generator and iterator. This would cause the error that re-iterating an exhausted generator. For instance the below generator, you can print out the values in a list:
some_gen = (i for i in range(10)) print(list(some_gen)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
And sometimes you may forget you have already iterated the generator once, and when you try to execute the below:
for x in some_gen: print(x)
You would not be able to see anything printed out.
To fix this issue, you shall save your result into a list first if you’re not dealing with a lot of data, or you can use the tee function from itertools module to create multiple copies of the generator so that you can iterate multiple times:
from itertools import tee # create 3 copies of generators from the original iterable x, y, z = tee(some_gen, n=3)
Conclusion
In this article, we have reviewed through some common Python mistakes that you may encounter when you start writing Python codes. There are definitely more mistakes you probably would make if you simply jump into the coding without understanding of the fundamentals. But as the old saying, no pain no gain, ultimately you will get there when you drill down all the mistakes and clear all the roadblocks.