A side effect is an observable change or interaction produced by an operation in addition to its returned value. In Python, a loop can be useful for what its commands do rather than what it returns. section 8.19
Recognize side effects when:
- A file loop assigns each line to a variable so the final assignment remains available after the loop. section 6.3
- A regular for loop inserts records into a database, making the external change more important than the loop's return value. section 8.19
- A comprehension constructs a new collection from expression values, so it is preferable when the result—not side effects—is the goal. section 8.19
Retaining the final file line
for one_line in open(filename): pass print(one_line) The loop's useful effect is assigning each line to one_line; after iteration, the final assignment remains available for printing. section 6.3























