Why triple dots notation in a class? Python Data Science Question


location_mean_age = new_df.groupby(“location”).age.mean()


This " groupby.age.mean" is a class object in python. I’m just trying to understand deeply since I’m new to python and just completed learning Python3 course. We learned that we can use single dot notation for a class object. How is it possible to give 3 dots works in a class? Can someone please explain?

This is known as a fluent interface:

Fluent interfaces in Python | Florian Einfalt – Developer

Where the methods return self (the instance/object), we can chain methods. Until we reach a “end method”, which will return an actual value

however, fluent interface have drawbacks:

Fluent Interfaces are Evil

So use fluent interfaces wisely.

1 Like

That link explains alot. Thank you. So it’s like a shortcut similar to list comprehension?

No, fluent interface is just a design pattern/concept.

1 Like

Another thing to note is that several of the drawbacks of fluent interfaces stem back to an assumption that you are programming in an OOP style. However, most data scientists and data analysts who are working with Pandas for exploratory analysis or data manipulation aren’t.

They are typically using a more functional programming style, as is common with scientific programming (the method chaining in Pandas is heavily inspired by R and dplyr).

I think you should be fine using the method chaining in Pandas, so long as you’re aware of what it’s doing.

For more information on how and why this technique is so heavily used in Pandas, check out this blog post (authored by a former Pandas core dev).