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.
Function arguments
The following example shows a case that breaks due to a careless choice in default parameters:
def spam(key, value, list_=[], dict_={}): list_.append(value) dict_[key] = value print('List: %r' % list_) print('Dict: %r' % dict_) spam('key 1', 'value 1') spam('key 2', 'value 2')
You would probably expect the following output:
List: [&apos...