Skip to main content
Loading page…
Python Mastery
Chapter 21
Lesson 4 of 4
Refactoring to Pythonic Code
Contents
Notes
Tutor
Note
Building on Anti-Patterns
Loading activity…
Previous
1 / 13
Continue
Contents
Notes
Discussion
Tutor
30 min
13 activities
0 of 13 done
In this lesson
1
Building on Anti-Patterns
2
Readability Is a Feature
3
Move 1: Loops to Comprehensions
4
Move 2: Nested Ifs to Guard Clauses
5
Move 3: if/elif Chains to Dict Dispatch
6
Graded Refactor: Loops to Comprehensions
7
Graded Refactor: Guard Clauses
8
Graded Refactor: Dict Dispatch
9
When to Use Dict Dispatch
10
Order of a Refactoring Pass
11
Your Refactoring Instinct
12
Best Practice: Refactor in Its Own Commit
13
Pitfall: Over-Comprehension
Objectives
Refactor C-style loops into list/dict comprehensions
Flatten nested if-chains into guard clauses
Replace long if/elif chains with dict dispatch
Apply multiple idioms in a single refactoring pass
Explain why readability is itself a feature
Chapter 21 · Pythonic Idioms & Writing Beautiful Code
Core Python Idioms
The Zen, PEP 8 & Naming
Common Anti-Patterns (and Fixes)
Refactoring to Pythonic Code
Previous: Common Anti-Patterns (and Fixes)
Next: Regular Expressions (re)