I need to do something like this:
abc = xyz()
abc.method1()
abc.method2()
abc.method3()
...
Is there a way to shorten this? Like:
abc= xyz()
abc.{method1(),method2(),method3(),...}
or something?
You can do this by ensuring that the instance functions (methods) each return a reference to the instance in which they are running (self)
class xyz:
def method1(self):
print('m1')
return self
def method2(self):
print('m2')
return self
def method3(self):
print('m3')
return self
abc = xyz()
abc.method1().method2().method3()
Output:
m1
m2
m3
Observation:
Whilst it can be done, I offer this merely as an answer to the original question. I do not condone the practice
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments