python - Multiple variables through one variable -
this question has answer here:
in python, have code this:
def var(a=1, b=2, c=3): return a, b, c
running gives outcome:
>>> import test >>> test.var() (1, 2, 3)
in case, 1, 2 , 3 default values want use. occasionally, want able modify or of variables. however, because have many possible var() statements different defaults , number of defaults. can var(a=1, b=2, c=3) or var(d=1, e=2, f=3, g=4) or var(h=0) , on.
i want modify these variables through single variable can feed command line, this:
>>> arguments = 'a=0, b=5, c=7'
but doesn't work.
>>> test.var(arguments) ('a=0, b=5, c=7', 2, 3)
it interprets 'arguments' variable being 'a' while leaving b , c untouched. how can make 'arguments' variable read literally def statement?
you can unpack tuple or dictionary arguments. means of following equal:
keyword_arguments = {'a':0, 'b':5, 'c':7} positional_arguments = (0, 5, 7) test.var(0, 5, 7) test.var(a=0, b=5, c=7) test.var(*positional_arguments) test.var(**keyword_arguments)
Comments
Post a Comment