Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 65e0802

Browse files
committedApr 9, 2025·
Clarified pure functions section and added immutability comment.
1 parent bc0b9fc commit 65e0802

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed
 

‎sections/software-design-1.qmd‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ twotimes = double
4848
type(twotimes)
4949
```
5050

51-
Note that when we print it to screen, we see that `prod` is of type `function`. We can see from the object address that it is the same object as `double`:
51+
When we print the type, we see that `twotimes` is of type `function`. We can see from the object address that it is the same object as `double`. It is the same object, with two different symbolic names:
5252

5353
```{python}
5454
print(twotimes)
@@ -84,7 +84,7 @@ wrapper(some_function)
8484

8585
::: {.callout-note}
8686

87-
Note how we passed the `some_function` as a variable name without the parentheses.
87+
Note how we passed the `some_function` as a variable name without the parentheses. This is a request for the function object, whereas, with paranethesis we are requesting to execute the function object code.
8888

8989
:::
9090

@@ -147,7 +147,7 @@ print(x)
147147

148148
So, you can see that writing a function that uses global variables can have effects outside of the scope of the function call. This can have drastic consequences on concurrent code, as the order in which function calls are made when operating concurrently are not deterministic, and so the impact of global variables will also not be deterministic.
149149

150-
A related issue arises when code in a function depends on its enclosing namespace, such as when a function is defined inside of another function. When resolving a variable, python first looks in the Local namespace, and then in the Enclosing namespace, Global namespace, and Built-in namespace. So, even if a variable is not defined locally, it might still be resolved by one of the other namespaces in surprising ways.
150+
A related issue arises when code in a function depends on its enclosing namespace, such as when a function is defined inside of another function. **When resolving a variable, python first looks in the Local namespace, and then in the Enclosing namespace, Global namespace, and Built-in namespace.** So, even if a variable is not defined locally, it might still be resolved by one of the other namespaces in surprising ways.
151151

152152
```{python}
153153
a = 3
@@ -163,11 +163,11 @@ A **pure function** is a function that depends only on its input arguments, and
163163

164164
In contrast, a non-pure function is a function in which the return value may change if the function is called repeatedly, typically because it depends on some particular state that affects the outcome but is not part of the input arguments. For example, the `time.time()` function returns different values based on the current state of the system clock.
165165

166-
Using a global variable in a function creates a side-effect that makes it an impure function, as would other operations that modify an external state variable.
166+
Using a global variable in a function creates a side-effect that makes it an impure function, as would other operations that modify an external state variable, or for example write data to disk that affect future calls to the function.
167167

168168
:::{.callout-important}
169169

170-
**Pure functions** make writing concurrent code much easier.
170+
**Pure functions** make writing concurrent code much easier. Along with **immmutable data** structures such as tuples they significantly reduce the complexity and efficiency of writing concurrent code safely.
171171

172172
:::
173173

0 commit comments

Comments
 (0)
Please sign in to comment.