Answer the question
In order to leave comments, you need to log in
Calling all class methods in one line. Maybe?
Python 2.7. There is a class with 5 methods and a constructor __init___ . In a certain situation, you need to call all the methods of the class. Is it possible to do this in one line in Python?
Java has a Builder like this one.
Answer the question
In order to leave comments, you need to log in
I don’t quite understand why, but in order to call functions in one line, it is enough to return self in the function at the end.
class S():
kk = 0
def m(self):
kk+=1
return self
def p(self):
print(self.kk)
return self
s = S().m().m().m().p().m().m().p()
import inspect
for _, m in inspect.getmembers(obj, predicate=inspect.ismethod): m()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question