Use variables from another class

How do you access a variable of one class from another?

Below is the code I'm working with and I need to be able to use thing1 and thing2 variables from the MainHandler (since you can only access "request" from that class).

I need this variable so that I can use the string that it returns below in the html.

class MainHandler(webapp2.RequestHandler):
    def get(self):
        p = Page()

        if self.request.GET:
            thing1 = self.request.GET['thing1']
            thing2 = self.request.GET['thing2']
            self.response.write(p.print_final())
            print thing1 + thing2
        else:
            self.response.write(p.print_this())
class Page(object):
    def __init__(self):
        self.stuff = """
<!DOCTYPE HTML>
<html>
    <head>
        <title>Give Us Yo INFO</title>
        <link href="css/main.css" rel="stylesheet">
    </head>
    <body>
        <header>
            <h1>Some Header</h1>
        </header>

        <div class="main-form">
            <form method="GET" action="">
                <h2>Your Juice Sheet</h2>

                <label>Name: </label>
                <input type="text" name="thing1">

                <label>Email: </label>
                <input type="text" name="thing2">

                <input type="submit" value="Submit"/>
            </form>
        </div>
    </body>
</html>
        """
        self.final = """
<!DOCTYPE HTML>
<html>
    <head>
        <title>Give us YO INFO</title>
        <link href="css/main.css" rel="stylesheet">
    </head>
    <body>
        <header>
            <h1>Some Header</h1>
        </header>

        <div class="result">
            <h2>Thing1</h2>
            <div class= "thing1-input">
                <h3>Name</h3>
                <p></p>
            </div>
            <div class= "thing2-input">
                <h3>Thing2</h3>
                <p></p>
            </div>
        </div>
    </body>
</html>
        """
    def print_this(self):
        page_content = self.stuff
        page_content = page_content.format(**locals())
        return page_content
    def print_final(self):
        final_page_content = self.final
        final_page_content = final_page_content.format(**locals())
        return final_page_content


app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

Answers


The variable thing1 and thing2 is local to the get method of the MainHandler class.

In other words, these values are not visible outside of the get method, so there is no real way to "get to them" right now.

However, since you are calling the print_final and print_this methods from get, you can pass the value of thing1 and thing2 to them, like this:

class MainHandler(webapp2.RequestHandler):
    def get(self):
        p = Page()

        if self.request.GET:
            thing1 = self.request.GET['thing1']
            thing2 = self.request.GET['thing2']

            self.response.write(p.print_final(thing1, thing2))
            # --------------------------------^^^^^^  ^^^^^^
            print thing1 + thing2
        else:
            self.response.write(p.print_this())

Then, modify the print_final method to accept these two extra arguments:

def print_final(self, thing1, thing2):
    final_page_content = self.final
    final_page_content = final_page_content.format(**locals())
    return final_page_content

On Page creation, pass reference of MainHandler. And make thing1 and thing2 an object variable (not just local).

class MainHandler(webapp2.RequestHandler):
    def get(self):
        p = Page(self)

        if self.request.GET:
            self.thing1 = self.request.GET['thing1']
            self.thing2 = self.request.GET['thing2']
            self.response.write(p.print_final())
            print self.thing1 + self.thing2
        ...

class Page(object):
    def __init__(self, handler):
        self.handler = handler
        # you can now use self.handler.thing1
        self.stuff = ...

Need Your Help

Oracle thin driver vs. OCI driver. Pros and Cons?

java oracle persistence oci

When you develop a Java application that talks to oracle DBs, there are 2 options right? One is oracle thin driver, and the other is OCI driver that requires its own installation (please correct if...

Android OGLES2.0 : All applications getting error

java android opengl-es opengl-es-2.0

I'm new to android SDK and programming using OGLES2.0. my problem is, most of the programs are not running on my PC.