How to get a unique id for bound methods?

Russell E. Owen rowen at cesmail.net
Tue Aug 23 18:55:44 EDT 2005


In article <mailman.3378.1124742573.10512.python-list at python.org>,
 "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Russell E. Owen wrote:
>
>>>>The current issue is associated with Tkinter. I'm trying to create a tk
>>>>callback function that calls a python "function" (any python callable
>>>>entity).
>>>>
>>>>To do that, I have to create a name for tk that is unique to my python
>>>>"function". A hash-like name would be perfect, meaning a name that is
>>>>always the same for a particular python "function" and always different
>>>>for a different python "function". That would save a lot of housekeeping.
>
>have you tried Tkinter's built-in _register method?
>
>>>> import Tkinter
>>>> w = Tkinter.Tk()
>>>> help(w._register)
>Help on method _register in module Tkinter:
>
>_register(self, func, subst=None, needcleanup=1) method of Tkinter.Tk 
>instance
>    Return a newly created Tcl function. If this
>    function is called, the Python function FUNC will
>    be executed. An optional function SUBST can
>    be given which will be executed before FUNC.

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.

It's a frustrating problem. There should be some simple way to get a 
unique hash-like identifier for any callable entity. If one were just 
using functions then id() would be fine. But bound methods are too 
different.

I'll use "hash" for now, but given that I"m not sure what hash is even 
doing, I should recode to something that I know works.

-- Russell



More information about the Python-list mailing list