Skip to main content

How to Call One Method After Another in Python with Method Chaining

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Cleaning up a name can take three steps: trim the spaces, capitalize the words, split it into parts. Python lets you write all three in one line, name.strip().title().split(), because each method call hands its result to the next. This guide shows how that works on Python 3.15.2, and when it breaks.

Syntax​

Syntax
<value>.<first method>().<second method>()
PartWhat it means
<value>The object you start with
.<first method>()Runs on <value> and returns a result
.<second method>()Runs on the first method's result, not on <value>

Python reads the line from left to right. A chain only works when every method in it returns an object that has the next method.

The Example​

A name typed with extra spaces and no capitals, " ada lovelace ", is trimmed, then capitalized, then split into its two words, one more method per line.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

chaining.py
name = " ada lovelace "

print(name.strip())
print(name.strip().title())
print(name.strip().title().split())

The chaining.py script in a macOS-style code window: name holds ada lovelace with spaces around it, and three prints call strip, then strip and title, then strip, title and split.

Each method comes from Python's string methods:

  • strip() will "Return a copy of the string with the leading and trailing characters removed", spaces by default.
  • title() will "Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase."
  • split() will "Return a list of the words in the string".

Step 2: Run It​

Terminal
uv run python chaining.py
Output
ada lovelace
Ada Lovelace
['Ada', 'Lovelace']

The terminal after uv run python chaining.py: ada lovelace, then Ada Lovelace, then the list Ada and Lovelace.

strip() returned new text without the spaces. title() ran on that text and returned Ada Lovelace. split() ran on that and returned a list of the two words. name itself never changed: each string method returns a new string. The command works unchanged in PowerShell.

Step 3: See a Chain Break on None​

list.sort() sorts the list itself and returns None, so nothing can follow it:

sort_chain.py
numbers = [3, 1, 2]
print(numbers.sort().reverse())
Terminal
uv run python sort_chain.py
Output
Traceback (most recent call last):
File "/Users/you/source/python-basics/sort_chain.py", line 2, in <module>
print(numbers.sort().reverse())
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'reverse'

The terminal after uv run python sort_chain.py: a traceback ending in AttributeError: NoneType object has no attribute reverse.

The documentation says why: "To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance)." .reverse() was then called on None, which has no such method.

Common Mistakes​

MistakeWhat happensDo this instead
Chaining after a method that changes the object in place, such as sort() or append()AttributeError: 'NoneType' object has no attribute ...Call it on its own line, or use a function that returns a new object, such as sorted()
Expecting the chain to change the variablename stays " ada lovelace "Assign the result: name = name.strip().title()
A chain too long to readHard to see which step failedSplit it over several lines with a variable per step

FAQ​

Can I break a long chain over several lines?​

Yes. Wrap it in parentheses and start each line with the next .method(); Python joins lines inside parentheses.

Does chaining work with my own classes?​

Yes, when each method returns an object, often self, that has the next method.

Conclusion​

name.strip().title().split() turned " ada lovelace " into ['Ada', 'Lovelace'], each call working on the previous one's result. numbers.sort().reverse() failed because sort() returns None.

Additional Resources​