Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 580
LinuxMercedes Avatar asked Feb 28 '11 09:02

LinuxMercedes


2 Answers

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
like image 151
Keith Avatar answered Oct 18 '22 03:10

Keith


Why not simply this ?

def stuff(n, f=None):
    if f is None:
        return
    return f(n)
like image 21
Nicolas Lefebvre Avatar answered Oct 18 '22 04:10

Nicolas Lefebvre