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()}
Radu
  • 1
  • Check out method chaining. There are several questions on SO related to it: https://stackoverflow.com/questions/41817578/basic-method-chaining – Sean Mar 28 '19 at 15:40

2 Answers2

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()
blhsing
  • 91,368
  • 6
  • 71
  • 106
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.

heemayl
  • 39,294
  • 7
  • 70
  • 76