user553947
user553947

Reputation:

Passing Argument to Python's Function

I have this function:

def fun(arg1=1, arg2=2, arg3=3, arg4=4)

Now if I want to call fun with arg1=8 then I will do this:

fun(8)

and if I want to call it with arg1 = 8 and arg2 = 9 then I think this will do (correct me if I am wrong):

fun(8,9) # LINE2

How to call fun if I want to call it with the fourth argument = 10, without passing other argument values (let another argument have default valued)?

Upvotes: 1

Views: 353

Answers (4)

Chaos Manor
Chaos Manor

Reputation: 1220

If you have:

def fun(arg1=1,arg2=2,arg3=3,arg4=4):
    print(arg1)
    print(arg2)
    print(arg3)
    print(arg4)

and you call

fun(arg4=10)

you will get

1
2
3
10

and it should be what you want to get

Upvotes: 1

jbrown
jbrown

Reputation: 374

fun(arg4=10)

You just have to reference the specific argument(s) by name.

Upvotes: 7

user225312
user225312

Reputation: 131807

>>> def fun(arg1=1,arg2=2,arg3=3,arg4=4):
        print arg4

>>> fun(arg4='I called you!')
I called you!

Just call the specific argument you want.

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386342

just provide the arguments you want and the others will get their default values:

fun(arg4=10)

Upvotes: 1

Related Questions