简体   繁体   English

在python中传递变量参数

[英]passing variable arguments in python

I need to define a function that may or may not take one argument of the 3 arguments defined in a function. 我需要定义一个函数,该函数可以或可以不接受函数中定义的3个参数中的一个。 However, I get an error message as invalid syntax. 但是,我收到一条错误消息,提示语法无效。

Now, if I make my third argument as variable [value3], I get an error message as 'float' object is not iterable. 现在,如果我将第三个参数设为变量[value3],则会收到一条错误消息,因为“ float”对象不可迭代。

Also, I have realized that when all the arguments are passed, it creates a tuple, which is unfavorable. 另外,我已经意识到,当所有参数都传递时,它会创建一个元组,这是不利的。

Could someone help me solve the problem? 有人可以帮我解决问题吗?

def createValues(value1, *value2, value3):
    value = dict()
    value["VALUE1"] = value1
    value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])


createValues(2000,21000001,1)
createValues(2000,,1)

What you want is default arguments . 您需要的是默认参数 This allows your function to be called by passing only a part of its parameters, and defaults the other to a pre-defined value. 这样就可以仅通过传递一部分参数来调用您的函数,而将其他参数默认设置为预定义的值。

For instance: 例如:

def createValues(value1=12, value2=27, value3=42):
    value = dict()
    value["VALUE1"] = value1
    value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])

will allow you to call your function by either of the following ways: 将允许您通过以下两种方式之一来调用函数:

>>> createValues(1, 2, 3)
1, 2, 3
>>> createValues()
12, 27, 42
>>> createValues(value2=1)
12, 1, 42
>>> createValues(0, 5)
0, 5, 42

Since you seem confused with the * unary operator, I suggest that you read a bit about arguments unpacking (for example, check this post ). 由于您似乎对*一元运算符感到困惑,因此建议您阅读一些有关参数解压缩的内容 (例如,查看此文章 )。

Besides, using two commas as in createValues(2000,,1) is in no way a valid syntax in Python. 此外,在createValues(2000,,1)使用两个逗号createValues(2000,,1)是Python中的有效语法。

From my beginners point of view, I see two ways : 从我的初学者的角度来看,我看到两种方式:

Using lists 使用清单

The arg of createValues become a list so you have as many values as you want. createValues的arg成为一个列表,因此您可以拥有任意多个值。

def createValues(values):
    value = dict()
    #Inserting
    for val in iter(values):
        index = str(values.index(val))
        value["VALUE"+index] = val

    #Printing
    for (key, value) in value.iteritems(): 
        print ("Key : " + key + " | Value : " + value)


createValues([2000,21000001,1])
createValues([2000,1])

Using default value 使用默认值

3 args for your specific case 针对您的具体情况的3个参数

def createValues(value1, value3, value2 = None):
    value = dict()
    value["VALUE1"] = value1
    if value2 is not None:
        value["VALUE2"] = value2
    value["VALUE3"] = value3

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])


createValues(2000,21000001,1)
createValues(value1 = 2000, value3 = 1)

You cannot call a function like this: 您不能调用这样的函数:

createValues(2000,,1)

You can however check if the argument is None and work with that. 但是,您可以检查参数是否为None并使用该参数。

def createValues(value1, value2, value3):
    value = dict()
    value["VALUE1"] = value1 or 0
    value["VALUE2"] = value2 or 0
    value["VALUE3"] = value3 or 0

    print (value["VALUE1"],value["VALUE1"],value["VALUE1"])

Now, you can use the function like this: 现在,您可以使用如下功能:

createValues(2000, None, 1)

I have used the default value 0 for any missing argument. 对于任何缺少的参数,我都使用默认值0。

Also, the arguments are not being converted into a tuple . 同样,参数也不会转换为tuple You are packing the arguments into a tuple when printing. 您在打印时将参数打包到一个元组中。 Try this instead (note the lack of parenthesis): 请尝试以下操作(请注意缺少括号):

print value["VALUE1"], value["VALUE1"], value["VALUE1"]

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

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