How to get a unique id for bound methods?

Russell E. Owen rowen at
Wed Aug 24 23:03:43 CEST 2005

In article <mailman.3448.1124861507.10512.python-list at>,
 "Fredrik Lundh" <fredrik at> wrote:

>Russell E. Owen wrote:
>> Having looked at it again, it is familiar. I copied it when I wrote my
>> own code. I avoided using at the time both because the initial
>> underscore suggested it was a private method and because it introduces
>> an extra function call.
>> _register has the same weakness that my code had when I used id(func) -- 
>> it uses the id of the function to generate the unique tk function name,
>> but it keeps no reference to that function.
>> Either Tkinter is clever about keeping a reference to each callable
>> around, or else it has the same bug I was seeing and it just doesn't
>> show up often enough to have been caught. I should take some time and
>> look into that.
>of course it keeps a reference to it; how else do you expect
>the Tcl command to find the right PyObjects?

Right. Of course.

I started out emulating the code for _register but over time made 
various incremental changes. Eventually I messed up and started taking 
the id of the wrong thing. I was able to fix that and all is well -- 
without having to use the mysterious hash function.

I also now keep a reference to the tk function name so I can properly 
delete it when finished. That eliminates a small memory leak.

Thanks for all your help.

-- Russell

More information about the Python-list mailing list