Python Chainable Class Methods
I want to do the following: pattern = cl().a().b('test').c() where cl is a class and a, b, c are class methods. After that I need to call pattern.to_string and it should output a
Solution 1:
Return the class instance at the end of each method and store the intermediate results in a class variable:
classMyClass:
result = None
defa(self):
# do things and store in self.resultself.result = ...
returnselfdefb(self, value):
# do things and store in self.resultself.result = ...
returnself
This allows you to chain the methods as desired: cl().a().b("test").c()
.
You can then obtain the result by looking at the value of instance.result
.
Post a Comment for "Python Chainable Class Methods"