Radu
Radu

Reputation: 1

Calling class/ object methods in sequence

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()}

Upvotes: 0

Views: 416

Answers (2)

blhsing
blhsing

Reputation: 107134

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()

Upvotes: 1

heemayl
heemayl

Reputation: 42147

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.

Upvotes: 0

Related Questions