Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Sending multiple values for one argument to a function

Tags:

python

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?)

like image 655
AMcNall Avatar asked Oct 23 '25 08:10

AMcNall


1 Answers

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)
like image 80
skyking Avatar answered Oct 24 '25 22:10

skyking