How to get a unique id for bound methods?

Fredrik Lundh fredrik at
Wed Aug 24 07:29:16 CEST 2005

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?

>>> import Tkinter
>>> w = Tkinter.Tk()
>>> def f():
...     pass
>>> import sys
>>> sys.getrefcount(f)
>>> w.register(f)
>>> sys.getrefcount(f)
>>> del f

(PyObject pointers to the callable and the interpreter object are
stored in a Tcl clientData record associated with the command)


More information about the Python-list mailing list