简体   繁体   English

Python-将一个参数的多个值发送给函数

[英]Python - Sending multiple values for one argument to a function

I am a few days new to Python so if this is silly please excuse me.. 我刚接触Python几天,所以如果这很傻,请原谅。

Is there a method to sending multiple variables to a single function? 有没有一种方法可以将多个变量发送到单个函数? As an example: 举个例子:

pe.plot_chart(conn,7760,'DataSource1',123,save=True)

This above function takes a connection to SQL where it pulls data for unique ID 7760 from datasource1 (uniqueid 123). 上面的函数连接到SQL,在SQL中它从数据源1(唯一队列123)提取唯一ID 7760的数据。 Can I use some method to send multiple criteria for the DataSource1 field? 我可以使用某种方法为DataSource1字段发送多个条件吗? eg 例如

pe.plot_chart(conn,7760,['DataSource1','DataSource2'],[123,345],save=True)

pe.plot_chart was created by me, so any modifications that have to be made to it to make it work are fine pe.plot_chart由我创建,因此必须对其进行任何修改才能正常工作

Is this type of operation possible to perform? 是否可以执行此类操作?

EDIT: Adding on some extra info. 编辑:添加一些额外的信息。

The plot_chart function.. well it plots a chart, and saves it to the location above. plot_chart函数..它很好地绘制了一个图表,并将其保存到上面的位置。 Each call of the function produces one graph, I was hoping that by sending multiple values for a parameter I could have the function dynamically add more series to the plot. 该函数的每次调用都会生成一个图形,我希望通过为参数发送多个值,我可以使该函数动态地向绘图添加更多序列。

So if I send 4 data sources to the function, I will end up with 4 lines on the plot. 因此,如果我向该函数发送4个数据源,则在绘图上将得到4条线。 For this reason I am not sure looping through a data source collection would be good (will just produce 4 plots with one line?) 因此,我不确定循环遍历数据源集合是否会很好(将只用一条线生成4个图吗?)

Yes you can send multiple arguments to a function in python, but that shouldn't be a surprise. 是的,您可以向python中的函数发送多个参数,但这并不奇怪。 What you cannot do is having positional arguments after a keyword argument, that is calls like f(1, foo=2, 3) is not allowed (your example is invalid for that reason). 您不能做的是在关键字参数之后添加位置参数,即不允许调用f(1, foo=2, 3) (由于该原因您的示例无效)。

Also you cannot supply multiple values to a single argument in a strict sense, but you can supply an list or tuple to a single argument, that is for example f(1, foo=(2, 3)) is acceptable and your function might interpret that as you are supplying two values to the foo argument (but in reality it's only one tuple). 另外,从严格意义上讲,您不能为单个参数提供多个值,但是可以为单个参数提供列表或元组,例如f(1, foo=(2, 3))是可以接受的,并且您的函数可能解释为您正在为foo参数提供两个值(但实际上它只是一个元组)。

The downside is that the function must be able to distinguish between a tuple as argument and what is intended as a single argument. 缺点是该函数必须能够区分作为参数的元组和作为单个参数的元组。 The easiest way is to insist on that the argument should be a tuple or at least iterable. 最简单的方法是坚持认为该参数应该是元组或至少是可迭代的。 The function would have to look somewhat like: 该函数必须看起来像:

def f(foo, bar):
    for x in foo:
        do_something(bar, x)

f(bar=fubar, foo=(arg1, arg2, arg3))
f((arg1, arg2, arg3), bar=fubar) # same as previous line
f((arg1, arg2, arg3), fubar) # same as previous line

another more advanced alternative would be to use keyword argument for everything except what would be the multiple arguments by using variable argument list, but this is somewhat clumpsy in python2 as you'll need to supply all arguments as positional unless you manually unpack the keywords arguments, in python3 there is some relief as you can force using of keyword arguments: 另一个更高级的替代方法是对关键字所有参数使用除所有参数之外的所有参数,这是通过使用可变参数列表实现的,但在python2中这有点笨拙,因为您需要将所有参数作为位置提供,除非您手动解压缩关键字参数,在python3中,您可以强制使用关键字参数来减轻一些负担:

def f(*args, bar=fubar):
    for x in args:
        do_something(bar, x)

f(arg1, arg2, arg3, bar=fubar) 
# f(fubar, arg1, arg2, arg3) is not allowed

and then every argument that is not a keyword argument (still those positional arguments has to be the first arguments) will end up in args, and the bar argument is required to be passed as keyword argument. 然后所有不是关键字参数的参数(仍然是那些位置参数必须是第一个参数)都将以args结尾,并且bar参数必须作为关键字参数传递。

In python2 the above would need to be: 在python2中,上述内容需要为:

def f(*args, **kwds):
    bar = kwds.get("bar", fubar)

    for x in args:
        do_something(bar, x)
data_sources = [data_source1, data_source2, data_source3]

for source in data_sources:
   pe.plotchart(connection, uniqueid = 7760, source...)

There's various ways to approach this - if you want to send an iterable (like a list) to your function once and have the function iterate through them, you can do that. 有多种方法可以解决此问题-如果您想一次向函数发送可迭代对象(如列表)并让函数遍历它们,则可以执行此操作。 You can also call the function from a loop. 您也可以从循环中调用该函数。 If the other parameters are going to change for each iteration, look into "zip", which is useful for pairing data for looping. 如果其他参数将在每次迭代中更改,请查看“ zip”,这对于将数据配对以进行循环很有用。

It's possible to pair data source specifications with unique IDs in your case. 在您的情况下,可以将数据源规范与唯一ID配对。 Here is a simple approach with lists of tuples: 这是使用元组列表的简单方法:

def myFunc(values):
    for v in values:
        print v[0], v[1]

myFunc([("hello", 1), ("world", 2)])

The list elements could also be expanded into classes if there is a need for more description for each line to plot. 如果需要对每条绘制线进行更多描述,则列表元素也可以扩展为类。 The benefit of this flip is that you are handling one list of line descriptors (which are represented by tuples), not loosely coupled "arguments". 此翻转的好处是您正在处理一个行描述符列表(由元组表示),而不是松散耦合的“参数”。

The output BTW is this: 输出的BTW是这样的:

hello 1
world 2

Your specific case would change into this 您的具体情况将变为

pe.plot_chart(conn,7760,[('DataSource1',123),('DataSource2',345)],save=True)

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

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