Skip to content

Commit 4112718

Browse files
authored
Add a more elaborate description to the side effects section
1 parent 9d712ab commit 4112718

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,24 @@ If you can do this, you will be happier than the vast majority of other programm
549549
**Bad:**
550550

551551
```python
552-
# Global variable referenced by following function.
553-
# If another function used this name, now it'd be an array and could break.
552+
# This is a module-level name.
553+
# It's good practice to define these as immutable values, such as a string.
554+
# However...
554555
name = 'Ryan McDermott'
555556

556557
def split_into_first_and_last_name() -> None:
558+
# The use of the global keyword here is changing the meaning of the
559+
# the following line. This function is now mutating the module-level
560+
# state and introducing a side-effect!
557561
global name
558562
name = name.split()
559563

560564
split_into_first_and_last_name()
561565

562566
print(name) # ['Ryan', 'McDermott']
567+
568+
# OK. It worked the first time, but what will happen if we call the
569+
# function again?
563570
```
564571

565572
**Good:**
@@ -586,6 +593,7 @@ class Person:
586593
def name_as_first_and_last(self) -> list:
587594
return self.name.split()
588595

596+
# The reason why we create instances of classes is to manage state!
589597
person = Person('Ryan McDermott')
590598
print(person.name) # 'Ryan McDermott'
591599
print(person.name_as_first_and_last) # ['Ryan', 'McDermott']

0 commit comments

Comments
 (0)