将变量值作为参数传递给python函数

时间:2022-03-23 05:09:06

I am trying pass variable values as arguments to a function which I am calling with in a for loop. Somehow before calling the function when I print the variable values they are showing fine but they are not getting passed into function as I am getting Index out of range :0 which means nothing is passed. Researched with no use...your help is really appreciated. Code is:

我正在尝试将变量值作为参数传递给我在for循环中调用的函数。不知何故,在我打印变量值之前调用函数它们显示正常但它们没有被传递到函数中,因为我得到的索引超出范围:0表示没有传递任何内容。研究没有用...你的帮助非常感谢。代码是:

 for clx in root.findall('sample'):
        CName = clx.find('Name').text
        No01 = clx.find('First').text
        No02 = clx.find('Second').text
        print "Cname provided is" +CName
        print "First is" +No01
        print "Second is" +No02
        createCluster(CName, No01, No02)

createCluster:

def createCluster(CName, No01, No02):
    print len(sys.argv)
    ClsName=sys.argv[0]
    Node01=sys.argv[1]
    Node02=sys.argv[2]

Error:

Traceback (most recent call last):
  File "<string>", line 20, in <module>
  File "createCluster.py", line 8, in createCluster
    ClsName=sys.argv[0]
IndexError: index out of range: 0

1 个解决方案

#1


0  

You are not extracting the arguments passed to your method properly. In Python, you simply just use the arguments that you defined in your method like this:

您没有正确提取传递给您的方法的参数。在Python中,您只需使用您在方法中定义的参数,如下所示:

def createCluster(CName, No01, No02):
    print(CName)
    print(No01)
    print(No02)

createCluster('a', 'b', 'c')

The above will output

以上将输出

a
b
c

For your issue on your usage of sys.argv that you are incorrectly using:

对于您使用sys.argv的问题,您使用错误:

Snippet from the documentation on sys.argv:

sys.argv文档中的代码段:

The list of command line arguments passed to a Python script.

传递给Python脚本的命令行参数列表。

In example of this would be when calling your code from a shell prompt:

例如,从shell提示符调用代码时:

python your_script.py a b c

python your_script.py a b c

Inside your_script.py you will ahve

在your_script.py里面你会的

import sys

script_name = sys.argve[0]
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]

Which will output:

哪个会输出:

/absolute/path/to/your_script.py
a
b
c

So, you see here that it ends up giving you the name of the script and all arguments.

所以,你在这里看到它最终会给你脚本和所有参数的名称。

This is maybe where your confusion came from between calling a script and calling the method in the script.

这可能是您在调用脚本和调用脚本中的方法之间产生混淆的地方。

#1


0  

You are not extracting the arguments passed to your method properly. In Python, you simply just use the arguments that you defined in your method like this:

您没有正确提取传递给您的方法的参数。在Python中,您只需使用您在方法中定义的参数,如下所示:

def createCluster(CName, No01, No02):
    print(CName)
    print(No01)
    print(No02)

createCluster('a', 'b', 'c')

The above will output

以上将输出

a
b
c

For your issue on your usage of sys.argv that you are incorrectly using:

对于您使用sys.argv的问题,您使用错误:

Snippet from the documentation on sys.argv:

sys.argv文档中的代码段:

The list of command line arguments passed to a Python script.

传递给Python脚本的命令行参数列表。

In example of this would be when calling your code from a shell prompt:

例如,从shell提示符调用代码时:

python your_script.py a b c

python your_script.py a b c

Inside your_script.py you will ahve

在your_script.py里面你会的

import sys

script_name = sys.argve[0]
arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]

Which will output:

哪个会输出:

/absolute/path/to/your_script.py
a
b
c

So, you see here that it ends up giving you the name of the script and all arguments.

所以,你在这里看到它最终会给你脚本和所有参数的名称。

This is maybe where your confusion came from between calling a script and calling the method in the script.

这可能是您在调用脚本和调用脚本中的方法之间产生混淆的地方。