Reputation: 529
I am a few days new to Python so if this is silly please excuse me..
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). Can I use some method to send multiple criteria for the DataSource1 field? e.g.
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
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. 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. For this reason I am not sure looping through a data source collection would be good (will just produce 4 plots with one line?)
Upvotes: 4
Views: 9344
Reputation: 10238
It's possible to pair data source specifications with unique IDs in your case. 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:
hello 1
world 2
Your specific case would change into this
pe.plot_chart(conn,7760,[('DataSource1',123),('DataSource2',345)],save=True)
Upvotes: 0
Reputation: 14360
Yes you can send multiple arguments to a function in python, but that shouldn't be a surprise. 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).
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).
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:
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.
In python2 the above would need to be:
def f(*args, **kwds):
bar = kwds.get("bar", fubar)
for x in args:
do_something(bar, x)
Upvotes: 3
Reputation: 2763
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.
Upvotes: 0