Skip to content

Commit 7ba3a22

Browse files
authored
Check 2 (ssciwr#14)
* more fixes - part 1 complete * more fixes - part 2 * update part 3 * update task description * part 4
1 parent c35226f commit 7ba3a22

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

Material_Part3_Formatter/example1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# find all png files in a folder
77
def find_files(path=None, pattern="*.png", recursive=True, limit = 20) -> list:
8-
"""Find image files on the file system
8+
"""Find image files on the file system.
99
1010
:param path:
1111
The base directory where we are looking for the images. Defaults to None, which uses the XDG data directory if set or the current working directory otherwise.

Material_Part3_Formatter/example2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22

3-
def area_circ(r_in):
4-
"""Calculates the area of a circle with given radius.
3+
def area_circ(r_in ):
4+
"""Calculate the area of a circle with given radius.
55
66
:Input: The radius of the circle (float, >=0).
77
:Returns: The area of the circle (float)."""

Material_Part4_Pitfalls/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
## Instantiation of mutable default keyword arguments in function calls
44

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).
66

77
## Naming the module
88

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).
1012

1113
## Exhausting iterators
1214

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`.
1418

1519
## Variable assignment in different scopes
1620

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).
1824

1925
## 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

Comments
 (0)