1

I am looking to pass a function to another function, as well as a named parameter. This is similar to the question asked here, except that it doesn't address named parameters. A question was asked about it in the comments but no reply.

Example:

def printPath(path, displayNumber = False):
    pass

def explore(path, function, *args):
    contents = function(*args)

print explore(path, printPath, path, displayNumber = False)

This gives the error:

TypeError: explore() got an unexpected keyword argument 'displayNumber'
Community
  • 1
  • 1
Discant
  • 113
  • 5
  • A more relevant question that is already discussed - http://stackoverflow.com/questions/5940180/python-default-keyword-arguments-after-variable-length-positional-arguments – shaktimaan Sep 07 '14 at 05:47

1 Answers1

4

You just have to allow explore to receive named parameters as well:

def printPath(path, displayNumber = False):
    pass

def explore(path, function, *args, **kwargs):
    contents = function(*args, **kwargs)

print explore(path, printPath, path, displayNumber = False)
StefanoP
  • 3,798
  • 2
  • 19
  • 26