Correctness anti-patterns
These anti-patterns can lead to bugs or unintended behavior if not addressed. We are going to discuss the most common of these anti-patterns and alternative, recommended ways and approaches. We are going to focus on the following anti-patterns:
- Using the
type()
function for comparing types - Mutable default argument
- Accessing a protected member from outside a class
Note that using IDEs such as Visual Studio Code or PyCharm or command-line tools such as Flake8 will help you spot such bad practices in your code, but it is important to know the recommendations and the reason behind each one.
Using the type() function for comparing types
Sometimes, we need to identify the type of a value through comparison, for our algorithm. The common technique one may think of for that is to use the type()
function. But using type()
to compare object types does not account for subclassing and is not as flexible as the alternative which is based on...