Search code examples
pythontornado

How to define different get functions according to the number of path elements in Python Tornado?


For example, I define two path in the rulelist:

("/application", app),
("/application/display/(\d+)", app)

And in my app class, I want to define two get functions separately for different paths:

def get(self):
    self.write("display app list")

def get(self, action, id):
    self.write("display app info by id")

Solution

  • If you create two methods by the same name, Python will always call the second one. This is how Python works.

    But you can create one single get method with default parameters:

    Example:

    def get(self, action=None, id=None):
        if action != None and id != None:
            # do something ...
        else:
            # do something else ...