Wrapper functions and arguments

Jeremy Sanders jeremy+complangpython at jeremysanders.net
Tue Oct 2 07:38:23 CEST 2007


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/



More information about the Python-list mailing list