Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4112718

Browse files
authored
Add a more elaborate description to the side effects section
1 parent9d712ab commit4112718

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

‎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
defsplit_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
defname_as_first_and_last(self) ->list:
587594
returnself.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)

[8]ページ先頭

©2009-2025 Movatter.jp