473,659 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Wrapper functions and arguments

One little issue I have is to write a little wrapper which can generally
pass standard and keyword arguments to a callee:

def a(x, y, z):
print x, y, z
def b(x, y, z='fruitbat')
print x, y, z

for func in a, b:
def wrapper(func=fu nc, *args, **argsk):
# do something
func(*args, **argsk)
x.append(wrappe r)

x[0](1, 2, 3)
x[1](1, 2)
....

Is there any way to do this? Can you capture arguments in a tuple and dict,
but still receive other keyword arguments? The only solution I found was to
implement wrapper as a class (like I would in c++):

class wrapper(object) :
def __init__(self, func):
self.func = func
def __call__(self, *args, **argsk):
self.func(*args , **argsk)

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Oct 2 '07 #1
1 1259
On Behalf Of Jeremy Sanders
def a(x, y, z):
print x, y, z
def b(x, y, z='fruitbat')
print x, y, z

for func in a, b:
def wrapper(func=fu nc, *args, **argsk):
# do something
func(*args, **argsk)
x.append(wrappe r)

x[0](1, 2, 3)
x[1](1, 2)
...

Is there any way to do this? Can you capture arguments in a
tuple and dict, but still receive other keyword arguments?
I think you're missing one level of wrapping.
>>def a(x, y, z):
print x, y, z
>>def b(x, y, z='fruitbat'):
print x, y, z
>>x = []
for func in a, b:
def wrapper(func):
def wrapped(*args, **kwds):
print "wrapped!"
func(*args, **kwds)
return wrapped
x.append(wrappe r(func))
>>x[0](1, 2, 3)
wrapped!
1 2 3
>>x[1](1, 2)
wrapped!
1 2 fruitbat
>>>
---
Regards,
Ryan Ginstrom

Oct 2 '07 #2

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

Similar topics

2
1986
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for my wrapper. This is acually part of the project, libxmlconf on sourceforge. The newest working version isn't there yet, and cvs is lagged by 6 hours or so. So if you think you want to have a try at this I can tgz the source for you. My...
15
3809
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET, though there may be some more benign explanation that escapes me. If you want the world to see your replies, then you should post them directly to USENET. However, I'll see them either way. After years of frustration with DAO, it dawned on me...
1
3797
by: Subodh | last post by:
Hi, We have legacy code in C/C++, I am writing a wrapper component in C++/ CLI that will allow using this legacy code functionality in C#, I have linked all my static libraries to this C++/CLI DLL project I have created a wrapper public static ref class in C++/CLI project that will expose functions for the C# client. Problem: when I use a function that returns a pointer or that has pointer
5
2132
by: GCRhoads | last post by:
I have some templated functions and I want to write a call wrapper class for it. I also want to pass the function object from this class to some functions that will then call the function (forwarded through my class object to the real function). I've looked around for sources on how to do this but I can't find anything. How do you do this? I would also appreciate any references to good sources about this topic if you have any. ...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.