Python Web 开发中的装饰器与类

发布于 2024-09-04 15:52:16 字数 783 浏览 2 评论 0原文

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

我很好奇这三种方法的优点。这些方法有什么主要优点或缺点?为了修正想法,这里有三个例子。

Bottle 使用装饰器:

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

Pylons 使用控制器类:

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

Tornado使用带有类型方法的请求处理程序类:

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

哪种风格是最佳实践?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲凉≈ 2024-09-11 15:52:16

实际上,您列出的三种方法中的每一种都有一个特定于每个项目的原因。

  • Bottle 试图让事情保持原状
    尽可能简单/直接
    对于程序员来说。带装饰器
    您不必担心的路线
    关于开发人员对 OOP 的理解。
  • Pylons 的开发目标是
    代码可重用且易于使用
    与 WSGI 风格的 HTTP 集成
    流程路由。因此,他们有
    选择了一种非常面向对象的组织方式
    路线。举个例子,你可以
    复制和将 HelloController 粘贴到任何
    Pylons 应用程序,它应该只是
    神奇地工作。即使所述应用程序是
    在某些情况下通过 WSGI 提供服务
    复杂的时尚。
  • 龙卷风还有另一个原因
    按照它的方式做事:
    Tornado基于epoll的IOLoop(与tornado.web.Application结合使用)
    将每个 RequestHandler 实例化为
    请求进来。通过保留每个
    RequestHandler 仅限于特定的
    GET 或 POST 这使得 IOLoop 能够
    快速实例化该类,
    处理请求,最后让
    它被垃圾收集。这保持
    它快速高效,且体积小
    无论如何,内存占用
    您的应用程序有许多 RequestHandler
    有。这也是 Tornado 比其他基于 Python 的 Web 服务器可以处理更多并发请求的原因(每个请求都有自己的实例)。

现在,说了这么多您应该知道您始终可以覆盖默认框架行为。例如,我写了一个 Tornado 的 MethodDispatcher 使其工作起来更像 Pylons(好吧,当我编写它时,我想到的是 CherryPy)。由于有一个大的 RequestHandler(而不是许多小的 RequestHandler),它会稍微减慢 Tornado 的速度(并稍微增加内存占用),但它可以减少应用程序中的代码量,并使其更易于阅读(当然,在我看来有偏见=)。

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

无戏配角 2024-09-11 15:52:16

各种框架都试图通过最好的代码(用于编写和阅读)来实现最佳性能。他们各自采用基于或围绕 MVC 或 MVT 的不同策略。

您关注的重点可能取决于个人品味。我的回答也将如此。我正在努力避免任何形式的圣战,因为可能存在我不知道的有效技术论据。

但我个人更喜欢将路由与控制器(django 的视图)分开,并将模板与控制器分开。它使控制器的重用变得非常简单。是的,我是 Django 用户。

因此,我真的不太喜欢 Bottle 的装饰器或将东西包装在巨大的类中。当我还是 ASP.NET 开发人员时,我曾经这样做过,但 Django 让我自由了。

The various frameworks are trying to achieve the best performance through the best code (for writing and reading). They each adopt different strategies based on or around MVC or MVT.

What you're focussing on will probably come down to personal taste. And so will my answer. I'm trying very hard to avoid any sort of holy war because there may be valid technical arguments that I just don't know about.

But I personally prefer to keep the routing separate from the controller (django's view) and templating separate from that. It makes reusing controllers really simple. Yeah, I'm a Django user.

As such, I'm really not a big fan of Bottle's decorators or wrapping things in great big hulking classes. I used to when I was an ASP.NET dev but Django set me free.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文