Common pitfalls
Python is a language meant to be clear and readable without any ambiguities and unexpected behaviors. Unfortunately, these goals are not achievable in all cases, and that is why Python does have a few corner cases where it might do something different than what you were expecting.
This section will show you some issues that you might encounter when writing Python code.
Scope matters!
There are a few cases in Python where you might not be using the scope that you are actually expecting. Some examples are when declaring a class and with function arguments, but the most annoying one is accidentally trying to overwrite a global
variable.
Global variables
A common problem when accessing variables from the global
scope is that setting a variable makes it local, even when accessing the global
variable.
This works:
>>> g = 1
>>> def print_global():
... print(f'Value: {g}')
>>> print_global()
Value: 1
...