从hello world看pyramid的配置

pyramid的配置分为强制式配置和声明式配置
强制式的helloworld是:
from paste.httpserver import serve
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello world!')

if __name__ == '__main__':
    config = Configurator()
    config.add_view(hello_world)
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')


声明式的helloworld则为:
from paste.httpserver import serve
from pyramid.response import Response
from pyramid.view import view_config

@view_config()
def hello(request):
    return Response('Hello')

if __name__ == '__main__':
    from pyramid.config import Configurator
    config = Configurator()
    config.scan()
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')


看起来两段代码差不多,但是声明式的配置在代码量多时的优势就体现出来了,强制式配置需要对每一个view进行添加,数目多了就容易忘记,声明式配置只需要在每一个需要的方法上面就加上注解@view_config(),更加灵活。

你可能感兴趣的:(python,pyramid)