0

Is there a way to call class/ object methods in a sequence without writing every time the "class.method" or "object.method"?

For example:
class ClassAWithALongName():
    @classmethod
    def do_stuff1()

    @classmethod
    def do_stuff2()
    @classmethod
    def do_stuff3()

Instead of:
ClassAWithALongName.do_stuff1()
ClassAWithALongName.do_stuff2()
ClassAWithALongName.do_stuff3()

Is there a way to do it like:
ClassAWithALongName{
                    do_stuff1(),
                    do_stuff2(),
                    do_stuff3()}
1

2 Answers 2

1

You can store the function objects in a list so that you can iterate through the list to call the function objects instead:

routine = [
    ClassAWithALongName.do_stuff1,
    ClassAWithALongName.do_stuff2,
    ClassAWithALongName.do_stuff3
]
for func in routine:
    func()
Sign up to request clarification or add additional context in comments.

Comments

0

One way would be to make it a fluid design, and return the class from each classmethod:

class ClassAWithALongName:
    @classmethod
    def do_stuff_1(cls):
        # Do stuffs
        return cls

    @classmethod
    def do_stuff_2(cls):
        # Do stuffs
        return cls

Now, you can chain the function calls:

ClassAWithALongName.do_stuff_1().do_stuff_2()   

Will this design meet your need, and go along your work is highly dependent on your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.