473,385 Members | 1,326 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,385 software developers and data experts.

Functions, callable objects, and bound/unbound methods

If I do this:

def f(self): print self

class c1: pass

setattr(c1, 'm1', f)

Then f is automagically transmogrified into the appropriate sort of
method depending on how it is used:
>>c1.m1
<unbound method c1.f>
>>c1().m1
<bound method c1.f of <__main__.c1 instance at 0x51e738>>
>>c1().m1()
<__main__.c1 instance at 0x51ec60>

Note that m1 gets passed a self argument.

The same automatic transmogrification does not happen if I use an
callable instance instead of an actual function object:

class callable:
def __call__(self, *args, **kw): return args, kw
>>setattr(c1, 'm2', callable())
c1.m2
<__main__.callable instance at 0x51e738>
>>c1().m2
<__main__.callable instance at 0x51e738>
>>c1().m2()
((), {})

Note that no selfarg has been passed to m2.

The reason I want to do this is that I want to implement a trace
facility that traces only specific class methods. I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that to
be an instance of a callable class instead of a function is that I need
a place to store the old value of the method so I can restore it, and I
don't want to start building a global data structure because that gets
horribly ugly, and a callable class is the Right Thing -- if there's a
way to actually make it work.

Is there? I tried the obvious things (like making callable inherit from
function, and adding im_func and im_self attribuetes) but they didn't
work.

Thanks,
rg
Dec 1 '06 #1
6 2386
Ron Garret <rN*******@flownet.comwrote:
I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that
to be an instance of a callable class instead of a function is that I
need a place to store the old value of the method so I can restore it,
and I don't want to start building a global data structure because
that gets horribly ugly, and a callable class is the Right Thing -- if
there's a way to actually make it work.

Is there? I tried the obvious things (like making callable inherit
from function, and adding im_func and im_self attribuetes) but they
didn't work.
Read "Functions and Methods" in
http://users.rcn.com/python/download/Descriptor.htm

You need to implement a __get__ method on your class.
Dec 1 '06 #2
Duncan Booth wrote:
Ron Garret <rN*******@flownet.comwrote:
I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that
to be an instance of a callable class instead of a function is that I
need a place to store the old value of the method so I can restore it,
and I don't want to start building a global data structure because
that gets horribly ugly, and a callable class is the Right Thing -- if
there's a way to actually make it work.

Is there? I tried the obvious things (like making callable inherit
from function, and adding im_func and im_self attribuetes) but they
didn't work.

Read "Functions and Methods" in
http://users.rcn.com/python/download/Descriptor.htm

You need to implement a __get__ method on your class.
See also

http://groups.google.com/group/comp....503c5b9c66226e

for an example and some discussion.

Michele Simionato

Dec 1 '06 #3
Ron Garret wrote:
The reason I want to do this is that I want to implement a trace
facility that traces only specific class methods. I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that to
be an instance of a callable class instead of a function is that I need
a place to store the old value of the method so I can restore it, and I
don't want to start building a global data structure because that gets
horribly ugly, and a callable class is the Right Thing -- if there's a
way to actually make it work.
If the only reason for a callable class is to save a single value (the
original function), you could instead store it as an attribute of the
wrapper function.

Kent
Dec 1 '06 #4
Ron Garret wrote:
The reason I want to do this is that I want to implement a trace
facility that traces only specific class methods. I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that to
be an instance of a callable class instead of a function is that I need
a place to store the old value of the method so I can restore it, and I
don't want to start building a global data structure because that gets
horribly ugly, and a callable class is the Right Thing -- if there's a
way to actually make it work.
If the only reason for a callable class is to save a single value (the
original function), you could instead store it as an attribute of the
wrapper function.

Kent
Dec 1 '06 #5
In article <45**************@kentsjohnson.com>,
Kent Johnson <ke**@kentsjohnson.comwrote:
Ron Garret wrote:
The reason I want to do this is that I want to implement a trace
facility that traces only specific class methods. I want to say:

trace(c1.m1)

and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that to
be an instance of a callable class instead of a function is that I need
a place to store the old value of the method so I can restore it, and I
don't want to start building a global data structure because that gets
horribly ugly, and a callable class is the Right Thing -- if there's a
way to actually make it work.

If the only reason for a callable class is to save a single value (the
original function), you could instead store it as an attribute of the
wrapper function.
I considered that, and I may yet fall back on it, but 1) I wanted to
understand how these things worked and 2) I need a way to tell when a
method has been traced, and isinstance(method, tracer) seems less
hackish to me than hasattr(method, 'saved_function').

rg
Dec 1 '06 #6
In article <11**********************@j44g2000cwa.googlegroups .com>,
"Michele Simionato" <mi***************@gmail.comwrote:
Duncan Booth wrote:
Ron Garret <rN*******@flownet.comwrote:
I want to say:
>
trace(c1.m1)
>
and have c1.m1 be replaced with a wrapper that prints debugging info
before actually calling the old value of m1. The reason I want that
to be an instance of a callable class instead of a function is that I
need a place to store the old value of the method so I can restore it,
and I don't want to start building a global data structure because
that gets horribly ugly, and a callable class is the Right Thing -- if
there's a way to actually make it work.
>
Is there? I tried the obvious things (like making callable inherit
from function, and adding im_func and im_self attribuetes) but they
didn't work.
Read "Functions and Methods" in
http://users.rcn.com/python/download/Descriptor.htm

You need to implement a __get__ method on your class.

See also

http://groups.google.com/group/comp....d/d691240a5cfe
bcdf/93503c5b9c66226e?lnk=gst&q=simionato+subclassing+F unctionType&rnum=1&hl=e
n#93503c5b9c66226e

for an example and some discussion.

Michele Simionato
Thanks!

rg
Dec 1 '06 #7

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...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
7
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
15
by: Paulo da Silva | last post by:
How do I program something like the folowing program? PYTHON claims it doesn't know about 'foo' when initializing K! The only way I got this to work was doing the init of K in the __init__ func....
3
by: Michael Schneider | last post by:
Hello All, I am comming back to python after being away for several years. I would like to use weak refs in an observer pattern implementation. The problme that I have seems to be that...
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...
5
by: Bern McCarty | last post by:
I have a DLL written in C++ (it's really C code that was adjusted to compile OK as C++) that I compile successfully into IL with the /CLR switch of Visual C 7.1. I use the resultant library...
6
by: tac-tics | last post by:
Python is a crazy language when it comes to object versatility. I know I can do: .... def __init__(self): .... pass .... print "fun" fun However, experimenting shows that...
20
by: vbgunz | last post by:
I remember learning closures in Python and thought it was the dumbest idea ever. Why use a closure when Python is fully object oriented? I didn't grasp the power/reason for them until I started...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.