473,388 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to get a unique id for bound methods?

I have several situations in my code where I want a unique identifier
for a method of some object (I think this is called a bound method). I
want this id to be both unique to that method and also stable (so I can
regenerate it later if necessary).

I thought the id function was the obvious choice, but it doesn't seem to
work. The id of two different methods of the same object seems to be the
same, and it may not be stable either. For instance:

class cls(object):
def __init__(self):
print id(self.meth1)
print id(self.meth2)
def meth1(self):
pass
def meth2(self):
pass

c = cls()
3741536
3741536
print id(c.meth1)
3616240
print id(c.meth2)
3616240

I guess that just means bound methods aren't objects in their own right,
but it surprised me.

The "hash" function looks promising -- it prints out consistent values
if I use it instead of "id" in the code above. Is it stable and unique?
The documentation talks about "objects" again, which given the behavior
of id makes me pretty nervous.

Any advice would be much appreciated.

-- Russell
Aug 19 '05 #1
12 2666
Russell E. Owen wrote:
The id of two different methods of the same object seems to be the
same, and it may not be stable either.
Two facts you're (apparently) unaware of are conspiring against you:

1) the "id" of an object is consistent for the lifetime of the object,
but may be reused after the object goes away

2) methods are bound on an as-needed basis and then normally discarded
(unless you do something to keep them around)

An illustration:

class cls(object):
def meth1(self):
pass
def meth2(self):
pass

c = cls()
m1 = c.meth1
print id(m1)
-1209779308
m2 = c.meth1
print id(m2)
-1209652732
I guess that just means bound methods aren't objects in their own right,
but it surprised me.
Nope, they're objects, they just don't tend to be around very long.
The "hash" function looks promising -- it prints out consistent values
if I use it instead of "id" in the code above. Is it stable and unique?
The documentation talks about "objects" again, which given the behavior
of id makes me pretty nervous.

Any advice would be much appreciated.


I think you'll get the best advice from this group if you tell us what
the larger problem is that you're trying to solve.
--
Benji York

Aug 19 '05 #2
On Fri, 19 Aug 2005 13:29:19 -0700, "Russell E. Owen" <ro***@cesmail.net> wrote:
I have several situations in my code where I want a unique identifier
for a method of some object (I think this is called a bound method). I
want this id to be both unique to that method and also stable (so I can
regenerate it later if necessary).
I thought the id function was the obvious choice, but it doesn't seem to
work. The id of two different methods of the same object seems to be the
same, and it may not be stable either. For instance:
The id function works, but you are applying it to transient objects, which
is what bound methods are unless you cause them to persist one way or another.
class cls(object):
def __init__(self):
print id(self.meth1)
print id(self.meth2)
def meth1(self):
pass
def meth2(self):
pass

c = cls()
3741536
3741536 This means that self.meth1 only existed long enough to be passed to id,
and when id was done with determining its id, self.meth1 was freed.
Then self.meth2 was created, and happened to use a representation space
with the same id as was used for self.meth1. If the two objects (bound methods
here) existed at the same time, they would be guaranteed not to have the same id
unless they were actually the same object.
print id(c.meth1)
3616240
print id(c.meth2)
3616240 This happened to re-use a representation space with another id.
I guess that just means bound methods aren't objects in their own right,
but it surprised me. No, they are objects in their own right. You were surprised by your
[mis]interpretation of the above results ;-)
The "hash" function looks promising -- it prints out consistent values
if I use it instead of "id" in the code above. Is it stable and unique?
The documentation talks about "objects" again, which given the behavior
of id makes me pretty nervous.

Any advice would be much appreciated.

If you want a particular bound method to have a stable and persistent id,
make it persist, e.g.,
class cls(object): ... def __init__(self):
... print id(self.meth1)
... print id(self.meth2)
... def meth1(self):
... pass
... def meth2(self):
... pass
... c = cls() 49219060
49219060 print id(c.meth1) 49219020 print id(c.meth2) 49219020

Ok, those were transient, now nail a couple of bound methods down: cm1 = c.meth1
cm2 = c.meth2
And you can look at their id's all you like:
print id(cm1) 49219020 print id(cm2) 49219060 print id(cm1) 49219020 print id(cm2) 49219060

But every time you just evaluate the attribute expression c.meth1 or c.meth2
you will get a new transient bound method object, with a new id:
print id(c.meth1) 49219180 print id(c.meth2) 49219180

But the ones we forced to persist by binding the expression values to cm1 and cm2
above still have the same ids as before:
print id(cm1) 49219020 print id(cm2)

49219060

So the question would be, why do you (think you ;-) need ids for
these bound methods as such? I.e., what is the "situation" in your code?

Regards,
Bengt Richter
Aug 19 '05 #3
Russell E. Owen wrote:
The "hash" function looks promising -- it prints out consistent values
if I use it instead of "id" in the code above. Is it stable and unique?
The documentation talks about "objects" again, which given the behavior
of id makes me pretty nervous.

I dont know how the hash of a bound method is calculated,but as the
function of the method is a stable and referenced object and as
instances lives are in your hands,then an id(self)^id(self.meth.im_func)
should be a chance for that 'hash' function.

def methodId(boundMethod):
return id(boundMethod.im_self)^id(boundMethod.im_func)

class cls(object):
def __init__(self):
print methodId(self.meth1)
print methodId(self.meth2)
def meth1(self):
pass
def meth2(self):
pass

c = cls()
print methodId(c.meth1)
print methodId(c.meth2)

I think this is giving what you expected.

Regards Paolino

___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
Aug 19 '05 #4
In article <ma***************************************@python. org>,
Benji York <be***@benjiyork.com> wrote:
Russell E. Owen wrote:
The id of two different methods of the same object seems to be the
same, and it may not be stable either.


Two facts you're (apparently) unaware of are conspiring against you:

1) the "id" of an object is consistent for the lifetime of the object,
but may be reused after the object goes away

2) methods are bound on an as-needed basis and then normally discarded
(unless you do something to keep them around)


Thank you and Bengt Richter. You both explained it very well.

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.

Does the built-in hash function actually do the job?

If I centralize all tk callback management and keep objects that
represent the tk callback around then I can avoid the whole issue. I was
hoping to avoid that, because it complicates housekeeping and adds a
risk of memory leaks (at least I think so; right now tk deallocates its
callback functions in the few cases I care about so I don't worry about
it.)

-- Russell

P.S. Paolino: thank you also for your kind reply. Your suggestion sounds
very useful if I only want a hash for a bound function, but in this case
since I want a hash for any callable entity I'm not sure it'll work.
Aug 19 '05 #5
On Fri, 19 Aug 2005 16:33:22 -0700, "Russell E. Owen" <ro***@cesmail.net> 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.

Why do you need a name? Can you post an example snippet that shows
a callback function being used with Tkinter as you would wish?
I have a feeling there is a much simpler solution than you are imagining ;-)

Regards,
Bengt Richter
Aug 20 '05 #6
In article <43*****************@news.oz.net>,
bo**@oz.net (Bengt Richter) wrote:
On Fri, 19 Aug 2005 16:33:22 -0700, "Russell E. Owen" <ro***@cesmail.net>
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.

Why do you need a name? Can you post an example snippet that shows
a callback function being used with Tkinter as you would wish?
I have a feeling there is a much simpler solution than you are imagining ;-)


Here is an example (simplified from my real code; I may have introduced
an error in the process). I could switch to the twisted framework, but
this code has been working very well and it saves my users from having
to install a 3rd party package.

-- Russell

def addTkCallback(tk, func):
tkName = "cb%d" % hash(func)
tk.createcommand(tkNname, func)
return tkName

class TkSocket(TkBaseSocket):
def __init__(self,
addr,
port,
binary=False,
readCallback = None,
stateCallback = None,
tkSock = None,
):
...
try:
# create the socket and configure it
self._sock = self._tk.call("socket", ...)
self._tk.call("fconfigure", self._sock, ...)

# add callbacks; the write callback is just used to detect
state
readName =addTkCallback(self._tk, self._doRead)
self._tk.call('fileevent', self._sock, "readable",
readName)
connName = addTkCallback(self._tk, self._doConnect)
self._tk.call('fileevent', self._sock, "writable", connName
except Tkinter.TclError, e:
raise RuntimeError(e)
Aug 22 '05 #7
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.
def func(): .... print "Hello"
.... name = w._register(func)
name '10768336func'
w.tk.call(name)

Hello
'None'

</F>

Aug 22 '05 #8
Russell E. Owen wrote:
I have several situations in my code where I want a unique identifier
for a method of some object (I think this is called a bound method). I
want this id to be both unique to that method and also stable (so I can
regenerate it later if necessary).

def persistent_bound_method(m): .... return m.im_self.__dict__.setdefault(m.im_func.func_name, m)
.... class A: .... def x(self):
.... return
.... a=A()
a.x is a.x False persistent_bound_method(a.x) is persistent_bound_method(a.x) True


Aug 23 '05 #9
In article <ma***************************************@python. org>,
"Fredrik Lundh" <fr*****@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.


Thanks. That looks like just the thing. I think I had seen it but was
deterred by the leading underscore (suggesting an internal method whose
interface might change). Still, I guess if it gets modified I can just
change my code.

-- Russell
Aug 23 '05 #10
In article <ma***************************************@python. org>,
"Fredrik Lundh" <fr*****@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
Aug 23 '05 #11
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) 2 w.register(f) '9571664f' sys.getrefcount(f) 3 del f
w.tk.call("9571664f")

'None'

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

</F>

Aug 24 '05 #12
In article <ma***************************************@python. org>,
"Fredrik Lundh" <fr*****@pythonware.com> 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
Aug 24 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kylotan | last post by:
Although I see lots of references to them in various documentation, I can't find a decent explanation of exactly what they are. I'm guessing that it's a reference to a method that remembers which...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
8
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or...
18
by: Neil | last post by:
I am using SQL 7 with an MS Access 2000 MDB front end, using bound forms with ODBC linked tables. In one form, the user needs to be able to check a box to select one or more records. This is...
3
by: Phil | last post by:
I am looking to set up a hyperlink control on a form to retrieve letters that correspond to a record on a form. That is, there may be 100 form records, and I would like each of those form records...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: Richard Albrecht | last post by:
I have been trying to figure out for days on how to read values from a Bound ListBox. The listBox gets the values from an Access Table. I can read values fine for Non-Bound ListBoxes, But the...
18
by: JJ | last post by:
Now I know this question has been asked many times, but I cannot seem to find a good site which summarises the methods possible in vb .net. I am after a way of producing a unique serial number...
1
by: srinivasan srinivas | last post by:
HI Peter, It works will for instance and class methods. But it doesn't work for static methods. Can you tel me how to pickle static methods as well?? Thanks, Srini ----- Original Message ----...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.