Home  >  Q&A  >  body text

python - flask中为何有这么多的直接返回‘一句话’调用的方法呢?

标题可能说得不是很清楚,还是上代码:

Flask.wsgi_app(self, environ, start_response):
    ctx = self.request_context(environ)

然后可以看到,实际上会调用

def request_context(self, environ):
        return _RequestContext(self, environ)
        

之后再进入到class _RequestContext(object): 的__init__函数中,后面就不再写了。

我的疑惑是,在第一句生成ctx的时候,为何要弄出一个request_context 方法来呢?这个方法就只有简单的一个返回语句,那么我直接在开始的时候实例化不就好了:ctx = _RequestContext(self, environ)
而且像这样的使用方式在flask中其他地方也还有很多,那么这样使用有什么明显的好处吗? (或者说像我那样写的直接返回的句子有什么明显的坏处吗?)

大家讲道理大家讲道理2740 days ago551

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:24:26

    This is a matter of design and taste, not a technical issue.

    Take the example you gave. We see that there is a layer of encapsulation here, but because the encapsulated content is too simple, it makes you wonder whether it is necessary. To answer this question, we have to think about why there is encapsulation? Whether it’s a function or a class, we may define them for the following reasons:

    • They provide a certain logical function for us to understand

    • This logic will be called frequently. In order to avoid duplication (DRY principle), we abstract it

    This example is in line with the above two items: flask needs a function to create application context, and it is used in many places.

    ➜  flask grep "\.request_context" -rin .
    ./app.py:1918:            with app.request_context(environ):
    ./app.py:1925:            ctx = app.request_context(environ)
    ./app.py:1948:            return self.request_context(builder.get_environ())
    ./app.py:1977:        ctx = self.request_context(environ)

    Another benefit is that the RequestContext 算是比较内部的一个类,大多数情况下用户不会(也不应该)直接使用它。而为了让用户可以创建这个类的对象,作者封装了 Flask.request_context() method can be regarded as the minimum interface principle (try to provide the smallest interface to users).

    Another advantage of encapsulation is that as long as the interface is fixed, the internal implementation can be changed at will. In your version, if the implementation of initialization or initialization changes, all callers do not need to change; otherwise, all callers must be modified accordingly. ctx = _RequestContext(self, environ),在我安装的版本里(Flask==0.12)这行代码是 ctx = RequestContext(self, environ)。虽然这里只是一个类名的简单变化,但是通过它我们可以明白,如果我们对 RequestContext

    Of course, the content encapsulated here is only one sentence. These benefits are not so obvious, and it even seems a bit far-fetched on my part. But I guess this is the result of the author's thinking, because

    is a relatively important class in Flask, and it is very likely to be modified in the future (add some attributes, change initialization parameters, etc.), encapsulate it with a layer, Can easily cope with possible future changes. RequestContextAfter all, an important thing in software engineering is to cope with changes.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:24:26

    This is the question of whether object-oriented member variables are visible to the outside world. What is operated here is the member variables of the class's member variables, which are not suitable for direct access.
    You can refer to property. What do you think are the advantages of property?
    Obviously, when the attribute you need is not obtained directly but obtained through calculation, you only need to modify the attribute acquisition method.

    reply
    0
  • Cancelreply