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

本文是小编为大家收集整理的关于python网络开发中装饰器与类的比较的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我注意到了Python Web Frameworks交易请求交易:装饰器,带有单个请求的方法的控制器类,以及使用方法/发布方法请求类.

我对这三种方法的优点感到好奇.这些方法中的任何一种有主要优势或缺点吗?要解决想法,这里有三个示例.

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

塔架使用控制器类:

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

tornado 使用请求处理程序类别类型的方法:

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

最好的做法是哪种样式?

推荐答案

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

  • 瓶子试图将事物保持为 简单/直接 对于程序员.与装饰师 对于路线,您不必担心 关于开发人员的理解.
  • 塔的开发目标是使 代码可重复使用,很容易 与WSGI风格的HTTP集成 过程路由.因此,他们有 选择了一种非常OOP的组织方式 路线.例如,你可以 复制和粘贴hellocontroller 塔塔应用程序,它应该只是 神奇地工作.即使说应用程序是 在某些人中通过WSGI服务 复杂的时尚.
  • 龙卷风还有另一个原因 按照自己的方式做事: 龙卷风的基于Epoll的Ioloop(与Tornado.Web.Application结合使用) 实例化每个请求者作为 请求进来.保留每个请求 请求Handler限于特定 获取或发布此允许Ioloop 快速实例化班级, 处理请求,最后让 收集垃圾.这保持了 它很快,很小 内存足迹不管如何 许多请求手您的应用程序 有.这也是龙卷风比其他基于Python的Web服务器更多的同时请求的原因(每个请求都有其自己的实例).

现在,说了所有事情,您应该知道,您总是可以覆盖默认框架行为.例如,我写了一个> MethodDispatcher 对于龙卷风而言,它使其更像塔(嗯,嗯,我写的时候我都想到了cherrypy).它减慢了龙卷风的量度很小(并略微增加了内存足迹),这是由于有一个大的请求者(与很多小型索引相反),但可以减少应用程序中的代码量,并使阅读更易于阅读. (当然,我有偏见的意见=).

其他推荐答案

各种框架试图通过最佳代码(用于写作和阅读)来实现最佳性能.他们每个人都根据MVC或MVT采用不同的策略.

您的关注可能会归结为个人品味.我的回答也是如此.我正在努力避免任何形式的圣战,因为我可能不知道有效的技术论点.

,但是我个人更喜欢将路由与控制器(Django的视图)分开,并与之分开.它使重复使用控制器非常简单.是的,我是Django用户.

因此,我确实不是对瓶子装饰者的忠实拥护者,也不是在大型笨拙的班级中包装的东西.我曾经是ASP.NET DEV的时候,但Django让我免费.

本文地址:https://www.itbaoku.cn/post/638469.html

问题描述

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

其他推荐答案

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.