Not even sure if module-level is correct here, but...
I have a Pylons project and within the model component I have a global variable, doc, in __init__.py
that I want to use from different Query objects. (doc is a Document handle on an XML file that I am using as a fake DB.) My question is, when does __init__.py
's scope end? Currently I am not freeing the Document with doc.unlink() and I am not sure where to put that.
The alternative design I was thinking about deals with making the consuming object (Query) have a class-level variable of this doc (i.e. make it a singleton). But it appears that the life of my Query object is such that doc always gets reallocated a new Document handle.
class Query(object):
doc = None
def __init__(self, content=None):
self.content = content
if self.doc == None:
self.doc = parse(os.path.join(config['app_conf']['xmldb'], "sample_search_result.xml"))
I can tell because the address of the elements within the Document keep changing.
Anyone want to help a noobie out?