简体   繁体   中英

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

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM