0

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?

tuck
  • 452
  • 5
  • 15

2 Answers2

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

Community
  • 1
  • 1
newtover
  • 31,286
  • 11
  • 84
  • 89
1

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
Ben
  • 2,065
  • 1
  • 13
  • 19
  • Your answer is only valid only because it doesn't matter which argument gets the default value for this particular sample function -- a fairly unique situation -- so isn't something generally applicable. – martineau Dec 28 '14 at 17:38