Python Tip #233 (of 365): Don't return self from a method just to allow method c...

@treyhunner@mastodon.social

Python Tip #233 (of 365):

Don't return self from a method just to allow method chaining.

In Python, methods that mutate an object usually return None.

Functions and methods either RETURN a thing or DO a thing, but usually not both.

You may be tempted to make a mutating method return self so that calls can be chained:

tasks = (
TodoList()
.add_task("Publish weekly email")
.add_task("Record screencast")
)

This works, but it's VERY unconvntional!

๐Ÿงต (1/4)

#Python #DailyPythonTip