473,396 Members | 2,076 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,396 software developers and data experts.

Wrapping a method twice

Hi all,
given the following code:

---
def append(method,bottom):
def wrapped(*args, **kwargs):
res = method(*args, **kwargs)
return bottom(res,*args, **kwargs)
setattr(method.im_class,method.__name__,wrapped)

def prepend(method,top):
def wrapped(*args, **kwargs):
top(*args, **kwargs)
return method(*args, **kwargs)
setattr(method.im_class,method.__name__,wrapped)
def test():
class C:
def f(self):
print "f"

def pre(self):
print "pre"

def post(res,self):
print "post"

prepend(C.f,pre)
append(C.f,post)
C().f()
---

how comes that test() only outputs:

pre
f

rather than what I expected:

pre
f
post

Thanks very much in advance,
cheers,
Nicolas
Jun 27 '08 #1
2 1221
Nicolas Girard napisał(a):
prepend(C.f,pre)
This wraps f, returns wrapped and binds it to C.f (but the
method.__name__ is still wrapped).
append(C.f,post)
This wraps wrapped (the one previously returned), returns wrapped (a
new one) and binds it to C.wrapped (since that is what its
method.__name__ says).
C().f()
If you try C().wrapped() it works as expected.

So the problem is in retaining the function name. Try:

def append(method,bottom):
def wrapped(*args, **kwargs):
res = method(*args, **kwargs)
return bottom(res,*args, **kwargs)
wrapped.__name__ = method.__name__
setattr(method.im_class,method.__name__,wrapped)

def prepend(method,top):
def wrapped(*args, **kwargs):
top(*args, **kwargs)
return method(*args, **kwargs)
wrapped.__name__ = method.__name__
setattr(method.im_class,method.__name__,wrapped)
Jun 27 '08 #2
On Jun 25, 1:52*pm, marek.ro...@wp.pl wrote:
>Try:

def append(method,bottom):
* * def wrapped(*args, **kwargs):
* * * * res = method(*args, **kwargs)
* * * * return bottom(res,*args, **kwargs)
* * wrapped.__name__ = method.__name__
* * setattr(method.im_class,method.__name__,wrapped)

def prepend(method,top):
* * def wrapped(*args, **kwargs):
* * * * top(*args, **kwargs)
* * * * return method(*args, **kwargs)
* * wrapped.__name__ = method.__name__
* * setattr(method.im_class,method.__name__,wrapped)
If you are using Python 2.5, consider functools.update_wrapper, which
also sets __module__,
__doc__ and other attributes.
Jun 27 '08 #3

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

Similar topics

13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
3
by: Randy | last post by:
Hello, How would this be done? I've looked at the datagrid itself and also the tableStyle and can't find out how to make text wrap in a cell. Thanks
1
by: Sangeetha | last post by:
I have a datagrid column which I want to expand when i type in some text. Initially I tried increasing the column width on the key down event of that DataGridTextBoxColumn, but it didn't work out as...
2
by: Andrzej Kaczmarczyk | last post by:
Hi I am experiencing something weird. maybe you could help me. I have two ineditable classes from outsource libraries: DataColumn and GridColumn I have built a wrapper class around...
3
by: Bruce Wood | last post by:
Maybe I'm going nuts, but I was so sure that adding the same method more than once to a delegate would result in only one entry on the delegate's call list: this.UpdateEnd += new...
4
by: Lawrence Oluyede | last post by:
I've never used metaclasses in real life before and while searching through the online Cookbook I found this gorgeous example: "Wrapping method calls (meta-class example)"...
4
by: Shark | last post by:
Hi, I have the following code: <div id="navAlpha"><div id="mainImage"><img src="/All/10001/main.jpg"></div> <div id="mainInfo"> // a list apart's mountain top <dl id="red"><dt>nick:...
0
by: bp_jobemail | last post by:
I'm trying to use PHP to wrap the output of an HTML form before it goes into a precompiled C cgi script. Essentially, the company that I work for uses a purchased precompiled c program for their...
4
by: TarTar | last post by:
Hello, I have already posted this problem, but I have not received any response yet. I will try to describe it again. We have a list control (e.g. DataList) and an ObjectDataSource on an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.