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?
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
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.
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?
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
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.