Python - Sending multiple values for one argument to a function -


i few days new python if silly please excuse me..

is there method sending multiple variables single function? example:

pe.plot_chart(conn,7760,'datasource1',123,save=true) 

this above function takes connection sql pulls data unique id 7760 datasource1 (uniqueid 123). can use method send multiple criteria datasource1 field? e.g.

pe.plot_chart(conn,7760,['datasource1','datasource2'],[123,345],save=true) 

pe.plot_chart created me, modifications have made make work fine

is type of operation possible perform?

edit: adding on info.

the plot_chart function.. plots chart, , saves location above. each call of function produces 1 graph, hoping sending multiple values parameter have function dynamically add more series plot.

so if send 4 data sources function, end 4 lines on plot. reason not sure looping through data source collection (will produce 4 plots 1 line?)

yes can send multiple arguments function in python, shouldn't surprise. cannot having positional arguments after keyword argument, calls f(1, foo=2, 3) not allowed (your example invalid reason).

also cannot supply multiple values single argument in strict sense, can supply list or tuple single argument, example f(1, foo=(2, 3)) acceptable , function might interpret supplying 2 values foo argument (but in reality it's 1 tuple).

the downside function must able distinguish between tuple argument , intended single argument. easiest way insist on argument should tuple or @ least iterable. function have like:

def f(foo, bar):     x in foo:         do_something(bar, x)  f(bar=fubar, foo=(arg1, arg2, arg3)) f((arg1, arg2, arg3), bar=fubar) # same previous line f((arg1, arg2, arg3), fubar) # same previous line 

another more advanced alternative use keyword argument except multiple arguments using variable argument list, clumpsy in python2 you'll need supply arguments positional unless manually unpack keywords arguments, in python3 there relief can force using of keyword arguments:

def f(*args, bar=fubar):     x in args:         do_something(bar, x)  f(arg1, arg2, arg3, bar=fubar)  # f(fubar, arg1, arg2, arg3) not allowed 

and every argument not keyword argument (still positional arguments has first arguments) end in args, , bar argument required passed keyword argument.

in python2 above need be:

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

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -