如何将 sys.argv[n] 传递给 Python 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17523219/
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
How to pass sys.argv[n] into a function in Python
提问by Greg
I am trying to pass "sys.argv[1]" into a function.
我试图将“sys.argv[1]”传递给一个函数。
#!/usr/bin/env/ python
import sys
def main():
test(sys.argv[1])
def test(sys.argv[1]):
print "Hello " + sys.argv[1]
./arg.py World
File "./arg.py", line 5
def test(sys.argv[1]):
^
SyntaxError: invalid syntax
Not sure where to go from here after scowering the Interwebs for a few hours. I have also tried to set the "sys.argv[1]" to a variable and tried passing that into the function and still no avail.
在浏览互联网几个小时后,不知道从这里去哪里。我还尝试将“sys.argv[1]”设置为变量并尝试将其传递到函数中,但仍然无济于事。
采纳答案by TerryA
sys.argv[1]
returns a string, so you can't add that as a parameter/argument.
sys.argv[1]
返回一个字符串,因此您不能将其添加为参数/参数。
I don't see why you would need this, just put no arguments at all:
我不明白你为什么需要这个,只是不提出任何论点:
def test():
print "Hello " + sys.argv[1]
You may have also gotten mixed up. Argument names do not need to be the same name as what you would use when calling the function. For example, if I had n = 5
, the function does not need to have an argument called n
. It can be anything.
你可能也搞混了。参数名称不需要与调用函数时使用的名称相同。例如,如果我有n = 5
,则该函数不需要具有名为 的参数n
。它可以是任何东西。
So you can do:
所以你可以这样做:
def test(myargument):
print "Hello " + myargument
I'm just going to give you a quick little "tutorial" on arguments.
我只是要给你一个关于论点的快速小“教程”。
def myfunc():
print "hello!"
Here we have a function. To call it, we do myfunc()
, yes? When we do myfunc(), it prints "hello!".
这里我们有一个函数。称之为,我们这样做myfunc()
,是吗?当我们执行 myfunc() 时,它会打印“你好!”。
Here is another function:
这是另一个函数:
def myfunc(word):
print word
This function takes an argument. So when we call it, we haveto add an argument. Here's an example of calling the function:
这个函数接受一个参数。所以当我们调用它时,我们必须添加一个参数。下面是一个调用函数的例子:
def myfunc(word):
print word
myword = 'cabbage'
myfunc(myword)
Notice how 'cabbage'
is set in the myword
variable. It does nothave to be the same as what we called the argument in the function. Infact, we don't even have to create a variable if we need. We can just do myfunc('cabbage')
.
注意变量中的'cabbage'
设置方式myword
。它不是必须是相同的就是我们俗称的函数的参数。事实上,如果需要,我们甚至不必创建变量。我们只能做myfunc('cabbage')
。
Also, in this example, you can only input oneargument, as when we def
ined the function, we only used one parameter. If you add two, you'll get something like myfunc takes one argument (two given)
.
另外,在这个例子中,你只能输入一个参数,因为在我们输入def
函数时,我们只使用了一个参数。如果你添加两个,你会得到类似myfunc takes one argument (two given)
.
We called the argument word
so we can use that in the function. Notice how I have print word
and not print myword
. It's as if we did word = myword
, but instead of doing that step, we use the argument name.
我们调用了参数,word
以便我们可以在函数中使用它。请注意我有print word
和没有print myword
。就好像我们做了word = myword
,但不是做那一步,我们使用参数名称。
回答by Harry.Chen
I think you misunderstand the declaration and call of a function. In your program,there is only declaration,missing the calling statement. As for passing parameters from command line,here is an example which I prefer:
我认为您误解了函数的声明和调用。在您的程序中,只有声明,缺少调用语句。至于从命令行传递参数,这是我更喜欢的一个例子:
import sys
def hello(v):
print 'hello '+v
def main(args):
hello(args[1])
if __name__ == '__main__':
main(sys.argv)
The program start to execute from the line 'if name== 'main' by calling the function defined previously and passing the sys.argv as parameter
通过调用先前定义的函数并将 sys.argv 作为参数传递,程序从 'if name== ' main'行开始执行