Andy Huang
Andy Huang

Reputation: 177

How to understand Tornado's dict in the URL handler?

The code snippets comes from Python Tornado framework. Given the following definitions, how to understand the function call.

definition:

class Application(object):
    def __init__(self, handlers=None, default_host="", transforms=None, wsgi=False, **settings):
        ...

function call:

Application([(r'/user/(.*)', ProfileHandler, dict(database=database)),])

Upvotes: 1

Views: 299

Answers (1)

Waterlink
Waterlink

Reputation: 2369

Lets break it down step by step:

Application([(r'/user/(.*)', ProfileHandler, dict(database=database)),]) - is a constructor call.

[(r'/user/(.*)', ProfileHandler, dict(database=database)),] - is the first parameter named handlers.

(r'/user/(.*)', ProfileHandler, dict(database=database)) - is specific handler, described by tuple of: route regex, handler class, any additional options for handler.

So dict(database=database) is additional options for first handler (3rd member of handler tuple). It seems to specify database to use.

Result of dict(database=database) will be the same as {'database': database}. You are allowed to pass any arbitrary keyword arguments to it to build your dictionary (example: dict(database=database, hello='world', environment='development', etc='test'), and so on).

dict keyword is an actual dictionary type name, so this is call of dictionary's constructor.

Upvotes: 3

Related Questions