简体   繁体   English

GAE为什么要使用类和self.response.out.write覆盖函数并进行打印?

[英]Why does GAE use classes and self.response.out.write over functions and print?

I'm a beginner and am wondering why we use self.response.out.write instead of print , and why we use classes, instead of functions, for the request handlers in the first place. 我是一个初学者,想知道为什么我们使用self.response.out.write而不是print ,为什么我们首先使用类而不是函数作为请求处理程序。 Are there any special reasons? 有什么特殊原因吗?

Using Methods 使用方法

Each handler class has methods with names like get and post , after the HTTP methods GET and POST etc. Those methods are functions that handle requests. 每个处理程序类在HTTP方法GETPOST等之后都有名称类似于getpost的方法。这些方法处理请求的函数。

Each request to your server will be routed to a request handler object, which is a new instance of some request handler class. 对服务器的每个请求都将路由到请求处理程序对象,该对象是某些请求处理程序类的新实例。 So, a request handler instance is created per request, and is garbage collected once its HTTP response is sent. 因此,将为每个请求创建一个请求处理程序实例,并在其HTTP响应发送后进行垃圾回收。

By inheriting from webapp2.RequestHandler , your handler classes get a bunch of functionality out the box for free. 通过从webapp2.RequestHandler继承,您的处理程序类可以免费获得大量功能。 For example, handler instances will have the data from the HTTP request parsed into dictionaries and bound to self as self.request.headers and self.request.body automatically. 例如,处理程序实例将具有从解析成词典和绑定到HTTP请求中的数据self作为self.request.headersself.request.body自动。

The webapp2.RequestHandler class also provides self.response , which is what you write your response data to. webapp2.RequestHandler类还提供self.response ,这就是您将响应数据写入其中的内容。

Once the new request handler instance is initialised, the inherited __init__ method calls the method that maps to the HTTP request method, so assuming a GET request, it calls self.get . 初始化新的请求处理程序实例后,继承的__init__方法将调用映射到HTTP请求方法的方法,因此,假设是GET请求,它将调用self.get The webapp2.RequestHandler class doesn't implement those methods; webapp2.RequestHandler类未实现这些方法; your derived class does. 您的派生类可以。

Responding 回应

Neither print nor the return value of the handler method are used here. 这里既不使用print也不使用handler方法的返回值。 You do not 'return a response' with this framework; 您不会使用此框架“返回响应”。 you write the response to the request handler instance's (inherited) self.response property. 您将响应写入请求处理程序实例的(继承)的self.response属性。

Your instance inherits self.response.out.write (which is aliased to self.response.write ), which concatenates its argument to the body of the response, initially an empty string. 您的实例继承了self.response.out.write (别名为self.response.write ),该实例将其参数连接到响应主体,最初是一个空字符串。

Note: You can call self.response.clear to clear the response body. 注意:您可以调用self.response.clear清除响应正文。

When you return from your handler method - get or post etc. - the return value is ignored. 从处理程序方法返回时(例如getpost等),返回值将被忽略。 The framework uses the state of self.response to automatically create and send a HTTP response for you. 该框架使用self.response状态自动为您创建并发送HTTP响应。

There's a bunch of subtleties that the framework takes care of behind the scenes too. 该框架在后台也处理了很多细节。

Classes Over Functions 函数上的类

The main advantage is in inheritance. 主要优点是继承。 Normally, you'll create a single BaseHandler class that derives from webapp2.RequestHandler . 通常,您将创建一个从webapp2.RequestHandler派生的BaseHandler类。 The BaseHandler class will contain the core functionality for your actual handlers. BaseHandler类将包含实际处理程序的核心功能。 It might include some logic for converting data into little JSON packages for a Web API, for example. 例如,它可能包含一些用于将数据转换为Web API的小JSON包的逻辑。 All of the classes that actually handle requests would then be derived from your BaseHandler . 所有实际处理请求的类都将从BaseHandler派生。

You want a custom base class for your handler classes to derive from mainly so you can edit that base class. 您希望自定义处理程序类的自定义基类主要从中派生,以便您可以编辑该基类。 You want that base class to inherit from webapp2.RequestHandler so that all your handler instances inherit the framework magic. 您希望该基类从webapp2.RequestHandler继承,以便您的所有处理程序实例都继承框架魔术。

There is enough slight of hand to make the whole thing confusing, but it is easy to make sense of once you get it, and does save a lot of trouble. 足够多的手就能使整个事情变得混乱,但是一旦掌握就很容易理解,并且确实省了很多麻烦。

Technically, you could achieve all of the above just using functions and dictionaries, but Python is classically object oriented, so it would be painful and weird. 从技术上讲,您可以仅使用函数和字典来实现上述所有功能,但是Python传统上是面向对象的,因此会很痛苦又很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM