DEV Community

Discussion on: Fluent interface in python

Collapse
 
orenovadia profile image
orenovadia

In my opinion, fluent APIs are rarely needed in Python.
Optional keyword arguments in python allow, in a single function call, to specify a subset of required arguments.
For instance, one may argue that this interface is easier to use than fluent API:

ExampleNotFluent(foo="new foo value, bar="bar value",...)

ExampleNotFluent can be immutable, great!

Fluent APIs are more convenient in Java for instance, where keyword arguments are not a thing, and every single argument for every function must be specified. It is very common for builders in java to be fluent, where the user may only specify some arguments but not all.

User.builder().firstName("Spider").lastName("man").city(..).build()

Having said that, there is at least one instance where I really appreciate a fluent API in python: a chain of transformations where the intermediate objects are unwanted (and might cause confusion).
This is common in data manipulation and very useful in the Pandas library.
Here is a small snippet from a blog post about pandas I was too lazy to read:
gist.github.com/adiamaan92/740405b...