I'm using Tornado to build a web server. I need to dispatch different requests into a same class. Here is an example:
application.add_handlers(r"^(example).*", [
(r"/(mark)/(auth)"), MarkHandler,
(r"/(mark)"), MarkHandler,
])
And the class MarkHandler
:
class MarkHandler(tornado.web.RequestHandler):
def get(self, mark): # /(mark)
print('1111')
def get(self, mark, auth): # /(mark)/(auth)
print('2222')
However, it doesn't seem to work...
When I visit the link: www.example.com/mark
, the server gives me an error:
TypeError: get() missing 1 required positional argument: 'auth'
What I need as above is impossible?
Python doesn't allow method overloading the way C++ or Java does. Defining the same method get
twice in Python simply overwrites the first method with the second one.
Instead, you want a default value for the second argument:
class MarkHandler(tornado.web.RequestHandler):
def get(self, mark, auth=None):
if auth is not None:
print('2222')
else:
print('1111')