There is a fairly simple template called Builder Pattern , where the methods basically return a link to the current object, so that instead of chaining calls to each other, they are linked by a link to the object.
The actual Builder template described in the Gang of Four book is a bit detailed (why create a builder), instead just return a self reference from each setXXX() for a clean chain of methods.
In Python, it might look something like this:
class Person: def setName(self, name): self.name = name return self
And you can create such a person:
p = Person() p.setName("Hunter") .setAge(24) .setSSN("111-22-3333")
Keep in mind that you really need to associate methods with them by touching pa().b().c() , because Python does not ignore spaces.
As @MaciejGol notes in the comments, you can assign p as a chain with spaces:
p = ( Person().setName('Hunter') .setAge(24) .setSSN('111-22-3333') )
I cannot say that this is the best style / idea for Python, but it is similar to how it will look in Java.
source share