Skip to content

Commit f09c9f1

Browse files
authored
Check 2 (ssciwr#15)
* more fixes - part 1 complete * more fixes - part 2 * update part 3 * update task description * part 4 * more fixes * add examples for part 5
1 parent 7ba3a22 commit f09c9f1

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# Backup files
132+
*.bak

Diff for: Material_Part4_Pitfalls/README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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](https://github.com/ssciwr/Python-best-practices-course/blob/main/Material_Part4_Pitfalls/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.
6+
7+
**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).
68

79
## Naming the module
810

@@ -25,4 +27,6 @@ Assigning a variable within a function shadows any assignment that may have happ
2527
## Closure variable binding
2628
Python uses late binding, resulting that in closures variables are only looked up once the inner function is called.
2729

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).
30+
**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).
31+
32+
**Task 1: Experiment with the different modules in this section and reproduce the error and the solution.**

Diff for: Material_Part5_BetterCoding/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ if __name__ == "__main__"
88
```
99
**Why?** Because, if you import your code as a module, all code that is not contained in a function or class will be run. Now, if you directly run the module,`__name__` will be set to `"__main__"`. But if the module is imported, `__name__` is set to the module's name. See the examples [module1.py](module1.py) and [module2.py](module2.py).
1010

11-
**Task:** Run the code in `module1.py` and `module2.py` and see what is run upon import and how `__name__` changes.
11+
**Task 1:** Run the code in `module1.py` and `module2.py` and see what is run upon import and how `__name__` changes.
12+
13+
14+
15+
16+
17+
## Demonstrations
18+
19+
I will demonstrate how to improve the readability in some code snippets.
20+
21+
## Further Examples
22+
Here are further examples, mainly based on [this video](https://youtu.be/C-gEQdGVXbk). Feel free to take a look if any of these suggestions may help you write better code in the future.
23+
24+
### Ternary Conditionals

Diff for: README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ Course date: Nov 8th 2022, 9:00AM - 1:00PM
2929
1. [PEP recommendations](Material_Part1_PEP/README.md)
3030
1. [Linting](Material_Part2_Linter/README.md)
3131
1. [Code formatting](Material_Part3_Formatter/README.md)
32-
1. Write better code: [Pitfalls](Material_part4_Pitfalls)
33-
1. Write better code: [Examples](Material_part5_Examples)
32+
1. Write better code: [Pitfalls](Material_Part4_Pitfalls)
33+
1. Write better code: [Examples](Material_Part5_BetterCoding)
34+
35+
## Additional Topics
36+
37+
For general coding best practices, you should always put your source code under version control, implement tests and a documentation, and also add a license to your code. It is not possible to discuss all of this in a short course - here we only discuss Python-specific content. The SSC offers [tailored courses](https://ssc.iwr.uni-heidelberg.de/events) that you can participate in. There is also a [block course](https://ssciwr.github.io/sustainable_development_course/) that touches on all these aspects; the block course takes place every year in early March.

0 commit comments

Comments
 (0)