|
2 | 2 |
|
3 | 3 | ## Instantiation of mutable default keyword arguments in function calls
|
4 | 4 |
|
5 |
| -Default arguments are only evaluated once: At the time the function is created. If you provide a mutable default keyword argument and then change it in the function, the next time the function is called without that keyword the default will point to the same address as in the first call; but the argument will have already changed, so the default in the first call and the default in the second call are different. Solution: Only provide non-mutable default arguments. See the [example](./mutable_default.py). |
| 5 | +Default arguments are only evaluated once: At the time the function is created. If you provide a mutable default keyword argument and then change it in the function, the next time the function is called without that keyword, the default will point to the same address as in the first call; but the argument will have already changed, so the default in the first call and the default in the second call are different. Solution: Only provide non-mutable default arguments. See the [example](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/mutable_default.py). |
6 | 6 |
|
7 | 7 | ## Naming the module
|
8 | 8 |
|
9 |
| -A source of errors can be naming a module the same as another module that is imported, in this example the module is named `math.py` but also imports math from the standard Python library; and function calls using methods from the math module will fail, as Python will look for those in the `math.py` file. Solution: Name your module file different than the modules that you are importing. See the [example](./math.py). |
| 9 | +A source of errors can be naming a module the same as another module that is imported, in this example the module is named `math.py` but also imports math from the standard Python library; and function calls using methods from the math module will fail, as Python will look for those in the `math.py` file. |
| 10 | + |
| 11 | +**Solution**: Name your module file different than the modules that you are importing. See the [example](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/math.py). |
10 | 12 |
|
11 | 13 | ## Exhausting iterators
|
12 | 14 |
|
13 |
| -Iterators and generators can be exhausted, meaning you can only use them once. Solution: If you create an iterator or a generator and you need it more than once you need to save it first. As in the [example](./exhaust_iterators.py) provided, the iterator is created using `zip`, and can be saved in a `list`. |
| 15 | +Iterators and generators can be exhausted, meaning you can only use them once. |
| 16 | + |
| 17 | +**Solution**: If you create an iterator or a generator and you need it more than once you need to save it first. As in the [example](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/exhaust_iterators.py) provided, the iterator is created using `zip`, and can be saved in a `list`. |
14 | 18 |
|
15 | 19 | ## Variable assignment in different scopes
|
16 | 20 |
|
17 |
| -Assigning a variable within a function shadows any assignment that may have happened in an outer scope. Solution: Pass the variable as an argument into the inner scope or use the return value of a new assignment. See the [example](assignment.py). |
| 21 | +Assigning a variable within a function shadows any assignment that may have happened in an outer scope. |
| 22 | + |
| 23 | +**Solution**: Pass the variable as an argument into the inner scope or use the return value of a new assignment. See the [example](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/assignment.py). |
18 | 24 |
|
19 | 25 | ## Closure variable binding
|
20 |
| -Python uses late binding, resulting that in closures variables are only looked up once the inner function is called. Solution: Make sure the referenced variables are either passed to the inner function or is set correctly in the surrounding scope. See the [example](closure.py). |
| 26 | +Python uses late binding, resulting that in closures variables are only looked up once the inner function is called. |
| 27 | + |
| 28 | +**Solution**: Make sure the referenced variables are either passed to the inner function or are set correctly in the surrounding scope. See the [example](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/closure.py). |
0 commit comments