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?

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.