Search code examples
pythonfunctionoptional-parametersoptional-arguments

Assign pass to a function in Python


I have a piece of code that defines a function that takes a function as an argument, like so:

def stuff(n, f):
  f(n)

Now, I want to provide some default value of f that does nothing. So I figured I'd use pass, like so:

def stuff(n, f = None):
  if(f is None):
    f = pass
  f(n)

But this does not compile. How should I be doing this?


Solution

  • The pass is a keyword for the interpreter, a place holder for otherwise nothing. It's not an object that you can assign. You can use a no-op lambda.

    f = lambda x: None