class NotFlask():
def init(self):
self.routes = {}
def route(self, route_str):
def decorator(f):
self.routes[route_str] = f
return f
return decorator
def serve(self, path):
view_function = self.routes.get(path)
if view_function:
return view_function()
else:
raise ValueError('Route "{}"" has not been registered'.format(path))
app = NotFlask()
@app.route("/")
def hello():
return "Hello World!"
print (app.serve("/"))
这个先从app=Notflask来看
创建实例,然后装饰器,这里运行了route函数,在app.routes里添加了一对键值,最后运行serve函数,用get方法找到“/”对应的函数,hello()。整个过程,即通过“/”查找对应函数这个过程。