开发者

Passing Argument to Python's Function

开发者 https://www.devze.com 2023-02-09 20:16 出处:网络
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:

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):

开发者_如何学JAVAfun(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)?


fun(arg4=10)

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


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

fun(arg4=10)


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号