Chaining calls (a.b().c())
Chaining calls means using the result of one method as the object you call the next method on.
Used in 118 lessons of the course, from 00.01.
In term.strip().lower() Python reads from left to right.
term.strip()runs first and returns a new string..lower()is then called on that new string.- The final result is what gets stored in the variable.
Each .name() takes what the previous step left behind. So order matters, and you can read the line like a recipe: first this, then this.
It is written this way to avoid intermediate variables you don't need. It works because strings are immutable: each method returns a new string instead of changing the original.
If a method returns None, the next link fails, because None doesn't have that method. In the course you will see this form in almost every lesson, starting with key = term.strip().lower().
Reach for it when: each step transforms the result of the previous one and you don't need to keep the intermediate values.
Examples
Common mistake
items = [3, 1, 2] result = items.sort().reverse() print(result)
Why: `sort()` changes the list in place and returns `None`. Then `.reverse()` is called on `None`, which doesn't have it.
How to write it: Use something that returns a value: sorted(items)[::-1], or call the methods on separate lines.
