提交 ec91a321 authored 作者: James Bergstra's avatar James Bergstra

modified Module to cache itself in the memo on build

上级 6c4df656
...@@ -197,7 +197,7 @@ class Component(object): ...@@ -197,7 +197,7 @@ class Component(object):
The return value of this function will show up in the Module graph produced by make(). The return value of this function will show up in the Module graph produced by make().
""" """
raise NotImplementedError raise NotImplementedError()
def make_no_init(self, mode=default_mode): def make_no_init(self, mode=default_mode):
""" """
...@@ -1147,6 +1147,8 @@ class Module(ComponentDict): ...@@ -1147,6 +1147,8 @@ class Module(ComponentDict):
self.__dict__["local_attr"][attr] = value self.__dict__["local_attr"][attr] = value
def build(self, mode, memo): def build(self, mode, memo):
if self in memo:
return memo[self]
for k,v in self.local_attr.iteritems(): for k,v in self.local_attr.iteritems():
self.__setattr__(k,v) self.__setattr__(k,v)
inst = super(Module, self).build(mode, memo) inst = super(Module, self).build(mode, memo)
...@@ -1170,6 +1172,7 @@ class Module(ComponentDict): ...@@ -1170,6 +1172,7 @@ class Module(ComponentDict):
inst.__dict__[new_methodname] = curried inst.__dict__[new_methodname] = curried
assert getattr(inst, new_methodname) == curried assert getattr(inst, new_methodname) == curried
#print 'ADDING METHOD', method, 'to', id(inst), new_methodname, getattr(inst, new_methodname) #print 'ADDING METHOD', method, 'to', id(inst), new_methodname, getattr(inst, new_methodname)
memo[self] = inst
return inst return inst
def _instance_initialize(self, inst, init = {}, **kwinit): def _instance_initialize(self, inst, init = {}, **kwinit):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论