This is nothing new, Martin Fowler propose that way of expose apis several years ago. This is only a tiny explanation and the reasons because i choosen in certain cases this kind of implementation for my personal projects.
class ExampleNonFluent(object): foo = None bar = None
Now if we want assing a value to foo and bar code will be as following:
my_class = ExampleNonFluent() my_class = "new foo value" my_class = "new bar value"
Changing the way of we implement our object with fluent interface will be similar to this:
class ExampleFluent(object): foo = None bar = None def cfg_foo(self, value): foo = value return self def cfg_bar(self, value): bar = value return self
Object implementation is more extense, yes but now building object will be more readable:
my_class = ExampleFluent()\ .cfg_foo("new foo value")\ .cfg_bar("new bar value")
What's your thoughts about this topic, place here your opinion, it can be a nice discussion :)
Level up every day