Pass parameter to function if passed by user python -
this 1 should simple. have function , call function package within function. want customize passing arguments package of function on whether user has passed arguments function. example code:
import somepackage sp def myfunc(foo, bar, baz=none, xad=false): # code stuff... # finally: if baz not none: sp.somefunc(data=foo, method=bar, aux=baz) else: sp.somefunc(data=foo, method=bar)
is there way replace last 4 lines 1 neat pythonic line? like:
def myfunc(foo, bar, baz=none, xad=false): # code stuff... # finally: sp.somefunc(data=foo, method=bar, [aux=baz if baz not none])
you can use argument unpacking this:
def func1(one, two, three): args = {"one": one, "two": two, "three": three} func2(*args) def func2(one, two, three): print "one = %s, 2 = %s, 3 = %s" % (one, two, three) if __name__ == "__main__": func1(one="does", two="this", three="work")
example run:
python foo.py 1 = does, 2 = this, 3 = work
you can of course modify args
see fit before passing on.
Comments
Post a Comment