在python web开发中的装饰器与类 [英] Decorators vs. classes in python web development

查看:171
本文介绍了在python web开发中的装饰器与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到Python Web框架处理请求处理的三个主要方式:装饰器,具有单个请求的方法的控制器类,以及使用GET / POST方法的请求类。



我很好奇这三种方法的美德。这些方法中有哪些主要优点或缺点?要解决想法,这里有三个例子。



Bottle < a>使用装饰器:

  @route('/')
def index():
return 'Hello World!'

>塔架使用控制器类:

  class HelloController (BaseController):
def index(self):
return'Hello World'

龙卷风使用请求处理程序类,其类型为:



pre> class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write(Hello,world)

哪种风格是最佳做法?

解决方案

实际上有一个理由您列出的三种方法,具体针对每个项目。




  • 瓶尝试将东西保持为
    尽可能简单/直接
    为程序员。使用装饰器
    为路线,您不必担心
    关于开发人员了解OOP。

  • 支柱开发目标是使
    代码重新使用并且容易地
    与WSGI风格的HTTP
    流程路由集成。因此,他们有
    选择了一种组织
    路由的非常OOP方式。举个例子,你可以
    copy&将HelloController粘贴到任何
    Pylons应用程序,它应该只是
    神奇地工作。即使所述应用程序是
    ,通过WSGI以一些
    复杂的方式提供。

  • 龙卷风还有另一个原因是
    以这样的方式做事情:
    龙卷风的基于epoll的IOLoop(与tornado.web.Application一起使用)
    将每个RequestHandler实例化为
    请求进来。通过将每个
    RequestHandler限制为一个特定的
    GET或POST这允许IOLoop到
    快速实例化类,
    处理请求,最后让
    得到垃圾回收。不管
    的许多RequestHandler你的应用程序
    有多少,这样可以使
    快速而高效。这也是为什么龙卷风可以处理比其他基于Python的Web服务器更多的同时请求的原因(每个请求都获得自己的实例)。



现在,已经说了一切,你应该知道,你可以总是覆盖默认的框架行为。例如,我写了一个 MethodDispatcher ,它使得它更像Pylons(好吧,当我写了CherryPy时)。由于有一个大的RequestHandler(而不是很多小的),它会减轻龙卷风的微量(并增加内存占用),但是它可以减少应用程序中的代码量,使其更容易阅读(在我偏见中,当然=)。


I've noticed three main ways Python web frameworks deal request handing: decorators, controller classes with methods for individual requests, and request classes with methods for GET/POST.

I'm curious about the virtues of these three approaches. Are there major advantages or disadvantages to any of these approaches? To fix ideas, here are three examples.

Bottle uses decorators:

@route('/')
def index():
    return 'Hello World!'

Pylons uses controller classes:

class HelloController(BaseController):
    def index(self):
        return 'Hello World'

Tornado uses request handler classes with methods for types:

 class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

Which style is the best practice?

解决方案

There's actually a reason for each of the three methods you listed, specific to each project.

  • Bottle tries to keep things as simple/straightforward as possible for the programmer. With decorators for routes you don't have to worry about the developer understanding OOP.
  • Pylons development goal is to make code re-usable and to be easily integrated with WSGI-style HTTP process routing. As such, they have chosen a very OOP way of organizing routes. As an example, you could copy & paste HelloController into any Pylons app and it should just magically work. Even if said app is being served up via WSGI in some complicated fashion.
  • Tornado has yet another reason for doing things the way it does: Tornado's epoll-based IOLoop (in conjunction with tornado.web.Application) instantiates each RequestHandler as requests come in. By keeping each RequestHandler limited to a specific GET or POST this allows IOLoop to quickly instantiate the class, process the request, and finally let it get garbage collected. This keeps it fast and efficient with a small memory footprint regardless of how many RequestHandlers your application has. This is also the reason why Tornado can handle so many more simultaneous requests than other Python-based web servers (each request gets its own instance).

Now, having said all that you should know that you can always override the default framework behavior. For example, I wrote a MethodDispatcher for Tornado that makes it work more like Pylons (well, I had CherryPy in mind when I wrote it). It slows down Tornado a tiny amount (and increases the memory footprint slightly) due to having one large RequestHandler (as opposed to a lot of small ones) but it can reduce the amount of code in your app and make it a little easier to read (In my biased opinion, of course =).

这篇关于在python web开发中的装饰器与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆