Executing the same code for get (self) as post (self)

It was mentioned in other answers about how the same code works for both def get(self), and def post(self)for any given request. I was wondering what methods the people I was thinking about are using:

class ListSubs(webapp.RequestHandler):
    def get(self):
        self._run()

    def post(self):
        self._run()

    def _run(self):
        self.response.out.write("This works nicely!")
+5
source share
4 answers

I would suggest both theoretical and practical reasons why the approach you use (reorganizing the general code into a separate method and calling it from the post and get methods) is superior to a seemingly simpler alternative to just have one of these two methods called another.

" A B" "" "" - , , , , B, , , ; A B ( / B), . , . A B C, .

: , . , : A, B C ( / C) , , A B. , , , .

( ): , , :

def amethod(self):
    return cmethod(self)

()

amethod = cmethod

( , ). , :

class ListSubs(webapp.RequestHandler):

    def _run(self):
        self.response.out.write("This works even better!")

    get = post = _run

, "" , ( get _run ..) (, post, get ..), , .

+11

, /, .

+2

:

class ListSubs(webapp.RequestHandler):
    def post(self):
        self.response.out.write("This works nicely!")
    def get(self):
        self.post()
+2

, , , - , . , HTTP GET - . POST. URL-, GET POST, , . W3c , GET vs. POST.

GET POST . POST GET . GET , , POST .

, , URL- (example.com/blog/post-vs-get/). get(), . POST URL-, post(), .

class ListSubs(webapp.RequestHandler):
    def post(self):
        comment = cgi.escape(self.request.get('comment'))
        ## Do magic with our comment.
        self.get() ## Go off and return the rendered page.
    def get(self):
        ## Get the blog post out of the data store and render a page.
        self.response.out.write("""<html>
              <body>
                <p>My awesome blog post!</p>
                <form method="post">
                  <h1>Comment</h1>
                  <textarea name="comment" rows="3" cols="60"></textarea>
                  <input type="submit" value="Comment">
                </form>
              </body>
            </html>""")

POST. / - . , .

+1

All Articles