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

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=func, *args, **argsk):
# do something
func(*args, **argsk)
x.append(wrapper)

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 1246
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=func, *args, **argsk):
# do something
func(*args, **argsk)
x.append(wrapper)

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(wrapper(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
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...
15
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,...
1
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...
5
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...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.