I've read this post about circular referencing in python, but I'm not 100% whether weakref is the way to go in my case.
So here is where I am.
I wrote a framework for file versionning. I have a File
class that I manipulate to have information about local revision and the server revision.
It would looks like this (I intentionally inverted the classes declarations):
class File(object):
def __init__(self, path):
self.path = path
self.db_object = db_query_file(path)
self.local = _LocalVersion(self)
self.latest = _LatestVersion(self)
class _Version(object):
def __init__(self, file):
self.file = file
def size(): pass
def checksum(): pass
class _LocalVersion(Version):
def __init__(self, file):
super(_LocalVersion, self).__init__(file)
self.info = get_info_from_local(file.path)
def size(): return local_size(self.file.path)
def checksum(): return local_checksum(self.file.path)
class _LatestVersion(Version):
def __init__(self, file):
super(_LatestVersion, self).__init__(file)
self.info = query_latest_info_from_db(file.db_object)
def size(): return self.info.size
def checksum(): return self.info.checksum
Now I suspect that none of my File
objects will ever be garbage collected as they are referenced by their very internal organs, and thus will always have a positive ref count.
The instances of _Version
have no purpose of existing when the File
instance must be deleted, of course.
Would it be safe and relevant to define _Version
as:
class _Version(object):
def __init__(self, file):
self.file = weakref.proxy(file)
Thanks for your comments, corrections and ideas!
Cheers O.