Search code examples
pythontornado

Setting class variables inside Tornado application


I have a Tornado application structured like below:

...

class Application(tornado.web.Application):
    def __init__(self):
        ...

class MyHandler(tornado.web.RequestHandler):
    def get():
        ...

class MyOtherHandler(tornado.web.RequestHandler):
    def post():
        ...

class MyClass(object):
    my_class_variable = {}
    ...

    def my_method_one():
        # access my_class_variable
        ...

    def my_method_two():
        # access my_class_variable
        ...

Inside which, I have a class variable my_class_variable inside my custom class. I want to set this class variable from outside of MyClass, e.g. inside one of the handlers. Hence my question, is it a good practice in a Tornado application to modify class variables directly within methods of handlers? e.g.

MyClass.my_class_variable[key] = value

Solution

  • Yes, that's fine. Handlers often need to interact with other classes, in various ways.