Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling multiple methods of an object in sequence

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading