user94628
user94628

Reputation: 3731

Import a variable in a function in a class in one module to another

I want help on how I would get a variable in a class function sent to another module in python. For example:

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('main.html')

    def post(self):
        event = self.get_argument('event')
        print event
        return event

How can I use the event variable in another python module?

Thanks

Upvotes: 0

Views: 1379

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 114098

make it self.event ...

the reason for this is that event only exists in the scope of post method as it is... it is created when you call post and destroyed when you exit post

this page may help explain scope and variable lifetimes for you

http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/

Module 1

class MainHandler(tornado.web.RequestHandler):
    event = None
    def get(self):
        self.render('main.html')

    def post(self):
        MainHandler.event = self.get_argument('event')
        print event
        return event

Module 2

from module1 import MainHandler
print MainHandler.event

Upvotes: 2

Related Questions