在以下代码中,Graph() 充当 Vertex 和 Edge 的代理——客户端仅通过 Graph() 访问 Vertex 和 Edge:
from rest import Resource
from elements import Vertex, Edge
class Graph(object):
def __init__(self,db_url):
self.resource = Resource(db_url)
self.vertices = Vertex
self.edges = Edge
g1 = Graph('http://localhost/one')
g2 = Graph('http://localhost/two')
Vertex 和 Edge 访问资源对象的最佳方式是什么,而不必将其作为参数传递给 Vertex 和 Edge?
我不想将它作为参数传递的原因之一是因为 Vertex 和 Edge 具有类方法,例如 create(),它们也需要访问资源对象。
Flask/Werkzeug 使用“context locals”(http://werkzeug.pocoo.org/docs/local/)——这是正确的方法,还是有更好的方法?