什么是python中的“参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14924825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are "arguments" in python
提问by
I am new to python and I am reading an online book. There is a chapter which explains the arguments of what they are and when they are used but I don't understand the explanations that well. Can anyone explain better what arguments are?
And please try to explain as simple as you can, because I am a beginner and English is not my native language
我是 Python 新手,正在阅读在线书籍。有一章解释了它们是什么以及何时使用它们的论点,但我不太理解这些解释。谁能更好地解释什么是论点?
请尽量解释得尽可能简单,因为我是初学者,英语不是我的母语
回答by Remco Haszing
Python functions have to kinds of parameters.
args (arguments) and kwargs (keyword arguments)
args are required parameters, while kwargs have default values set
Python 函数必须有多种参数。
args(参数)和 kwargs(关键字参数) args 是必需参数,而 kwargs 设置了默认值
The following function takes arg 'foo' and kwarg 'bar'
以下函数采用 arg 'foo' 和 kwarg 'bar'
def hello_world(foo, bar='bye'):
print(foo)
print(bar)
This is how you can call the function
这是您可以调用该函数的方式
>>> hello_world('hello')
hello
>>> hello_world('hello', bar='cya')
hello
cya
回答by chepner
An argument is simply a value provided to a function when you call it:
参数只是在调用函数时提供给函数的值:
x = foo( 3 ) # 3 is the argument for foo
y = bar( 4, "str" ) # 4 and "str" are the two arguments for bar
Arguments are usually contrasted with parameters, which are names used to specify what arguments a function will need when it is called. When a function is called, each parameter is assigned one of the argument values.
参数通常与参数形成对比,参数是用于指定函数在调用时需要哪些参数的名称。当一个函数被调用时,每个参数被分配一个参数值。
# foo has two named parameters, x and y
def foo ( x, y ):
return x + y
z = foo( 3, 6 )
foois given two arguments, 3 and 6. The first argument is assigned to the first parameter, x. The second argument is assigned to the second parameter, y.
foo给定两个参数,3 和 6。第一个参数分配给第一个参数,x。第二个参数分配给第二个参数,y。

