Caveats in Python
Learn about the potential problems with using some of the idioms in Python.
We'll cover the following
Besides understanding the main features of the language, being able to write idiomatic code is also about being aware of the potential problems of some idioms and how to avoid them. In this lesson, we'll explore common issues that might cause us some long debugging sessions if they catch us off guard.
Most of the points discussed in this lesson are things to avoid entirely. There is almost no possible scenario that justifies the presence of the antipattern (or idiom, in this case). Therefore, if we find this on the code base we are working on, feel free to refactor it in the way that is suggested. If we find these traits while doing a code review, this is a clear indication that something needs to change.
Mutable default arguments
Simply put, don't use mutable objects as the default arguments of functions. If we use mutable objects as default arguments, we'll get results that are not the ones we expect.
Consider the following erroneous function definition:
Get hands-on with 1400+ tech skills courses.