Design thought for callbacks

Cem Karan cfkaran2 at gmail.com
Tue Feb 24 06:00:55 EST 2015


On Feb 23, 2015, at 7:29 AM, "Frank Millman" <frank at chagford.com> wrote:

> 
> "Cem Karan" <cfkaran2 at gmail.com> wrote in message 
> news:A3C11A70-5846-4915-BB26-B23793B65670 at gmail.com...
>> 
>> 
>> Good questions!  That was why I was asking about 'gotchas' with WeakSets 
>> originally.  Honestly, the only way to know for sure would be to write two 
>> APIs for doing similar things, and then see how people react to them.  The 
>> problem is, how do you set up such a study so it is statistically valid?
>> 
> 
> Just in case you missed Steven's comment on my 'gotcha', and my reply, it is 
> worth repeating that what I reported as a gotcha was not what it seemed.
> 
> If you set up the callback as a weakref, and the listening object goes out 
> of scope, it will wait to be garbage collected. However, as far as I can 
> tell, the weakref is removed at the same time as the object is gc'd, so 
> there is no 'window' where the weakref exists but the object it is 
> referencing does not exist.
> 
> My problem was that I had performed a cleanup operation on the listening 
> object before letting it go out of scope, and it was no longer in a valid 
> state to deal with the callback, resulting in an error. If you do not have 
> that situation, your original idea may well work.

Thank you Frank, I did read Steve's comment to your reply earlier, but what you said in your original reply made sense to me.  I don't have control over user code.  That means that if someone wants to write code such that they perform some kind of cleanup and are no longer able to handle the callback, they are free to do so.  While I can't prevent this from happening, I can make it as obvious as possible in my code that before you perform any cleanup, you also need to unregister from the library.  That is my main goal in developing pythonic/obvious methods of registering callbacks.

Thanks,
Cem Karan


More information about the Python-list mailing list