Default argument to Python attribute that is a function

Question:

I was wondering if it is possible in Python to specify a default argument to a function attribute in Python (I know this is not the right terminology so here is an example):

def foo(x, y): 
    return x + y

my_foo = foo(y=50)

my_foo(25) #returns 75

Does this sound possible?

Asked By: tuck

||

Answers:

You’d do it in the function definition:

def foo(x, y=50):
    return x+y

if y isn’t specified 50 is the default value:

print foo(25) # 25 is the value for x, y gets the default 50
Answered By: Ben
from functools import partial
def foo(x, y): return x + y
my_foo = partial(foo, y=50)
my_foo(100)
Out[433]: 150

But you should know about this.

Answered By: newtover
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.