在使用Twisted.web模块搭建web服务时,发现要定义isLeaf
变量。 很奇怪。这个变
量出现的莫名其妙,所以就找了相关的资料看。也有了一些理解。
引用Stack Overflow上的回答:
See twisted.web.resource.IResource.isLeaf documentation --
Signal if this IResource implementor is a "leaf node" or not. If True, getChildWithDefault will not be called on this Resource.
The way Twisted finds a resource to render is by splitting the path into segments, and calling "getChildWithDefault" on the root, and then whatever the root returns and so on. It stops if it either runs out of segments, or a "leaf" (i.e., isLeaf=True) resource is found.
At that point, it will call the render method on the resource. In a leaf resource, the renderer will often want to look at the "request.postpath" attribute -- stashed there is the list of segments that have not been used up to find the resource.
In general, only resources that are leaves are rendered; this can be because isLeaf is set to True or because when traversing the resource hierarchy, that resource is where we are when the URL runs out. However, when isLeaf is True for a resource, its getChild method is never called. Thus, for resources that have children, isLeaf cannot be set to True.
参考资料: