简体   繁体   English

将参数传递给 Python 的函数

[英]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:现在如果我想用 arg1=8 调用 fun 那么我会这样做:

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):如果我想用arg1 = 8arg2 = 9调用它,那么我认为这可以做到(如果我错了,请纠正我):

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)?如果我想用第四个argument = 10调用它而不传递其他参数值(让另一个参数具有默认值),如何调用fun

fun(arg4=10)

您只需要按名称引用特定参数。

只需提供您想要的参数,其他参数将获得它们的默认值:

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它应该是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM