Python Tornado如何根据路径元素个数定义不同的get函数?
How to define different get functions according to the number of path elements in Python Tornado?
比如我在rulelist中定义了两条路径:
("/application", app),
("/application/display/(\d+)", app)
而在我的应用程序 class 中,我想为不同的路径分别定义两个 get 函数:
def get(self):
self.write("display app list")
def get(self, action, id):
self.write("display app info by id")
如果您创建两个同名的方法,Python 将始终调用第二个。这就是 Python 的工作原理。
但是您可以使用默认参数创建一个 get
方法:
示例:
def get(self, action=None, id=None):
if action != None and id != None:
# do something ...
else:
# do something else ...
比如我在rulelist中定义了两条路径:
("/application", app),
("/application/display/(\d+)", app)
而在我的应用程序 class 中,我想为不同的路径分别定义两个 get 函数:
def get(self):
self.write("display app list")
def get(self, action, id):
self.write("display app info by id")
如果您创建两个同名的方法,Python 将始终调用第二个。这就是 Python 的工作原理。
但是您可以使用默认参数创建一个 get
方法:
示例:
def get(self, action=None, id=None):
if action != None and id != None:
# do something ...
else:
# do something else ...