"Asaf Las" <roeg...@gmail.com> wrote in message news:9729ddaa-5976-4e53-8584-6198b47b6...@googlegroups.com... > On Wednesday, January 22, 2014 10:56:30 AM UTC+2, Frank Millman wrote: >> >> class MainObject: >> def __init__(self, identifier): >> self._del = delwatcher('MainObject', identifier) >> class delwatcher: >> def __init__(self, obj_type, identifier): >> self.obj_type = obj_type >> self.identifier = identifier >> log('{}: id={} created'.format(self.obj_type, self.identifier)) >> def __del__(self): >> log('{}: id={} deleted'.format(self.obj_type, self.identifier)) >> If you do find that an object is not being deleted, it is then >> trial-and-error to find the problem and fix it. It is probably a circular >> reference >> >> Frank Millman > > Thanks Frank. Good approach! > > One question - You could do: > class MainObject: > def __init__(self, identifier): > self._del = delwatcher(self) > then later > > class delwatcher: > def __init__(self, tobject): > self.obj_type = type(tobject) > self.identifier = id(tobject) > ... > > when creating delwatcher. Was there special reason to not to use them? > is this because of memory is reused when objects are deleted > and created again so same reference could be for objects created > in different time slots? >
I read Dave's reply, and he is correct in saying that id's are frequently re-used in python. However, in this particular case, I think you are right, it is safe to use the id to identify the object. An id can only be re-used if the original object is deleted, and that is the whole point of this exercise. We expect to see the id come up in a 'created' message, and then the same id appear in a 'deleted' message. If this happens, we are not concerned if the same id reappears in a subsequent 'created' message. Frank -- https://mail.python.org/mailman/listinfo/python-list