使用其他类中的变量

时间:2015-12-03 07:43:51

标签: python class variables

如何从另一个类访问一个类的变量?

以下是我正在使用的代码,我需要能够使用thing1中的thing2MainHandler个变量(因为您只能访问&#34) ;请求"来自该课程。)

我需要这个变量,以便我可以在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)

2 个答案:

答案 0 :(得分:0)

在创建Page时,传递MainHandler的引用。并使thing1thing2成为对象变量(不仅仅是本地)。

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 = ...

答案 1 :(得分:0)

变量thing1thing2get类的MainHandler方法的本地。

换句话说,这些值在get方法之外是不可见的,所以现在没有真正的方法可以“到达它们”。

但是,由于您从print_final调用了print_thisget方法,因此您可以将thing1thing2的值传递给它们,例如这样:

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())

然后,修改print_final方法以接受这两个额外的参数:

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