Firstly I have already seen a number of similar questions, although they were not exactly my question. I am already familiar with *args and **kwargs.
Question Explanation:
I usually use positional arguments when calling a function. However, I often find myself needing to pass a plethora of arguments into a function, so using positional arguments has gotten quite burdensome. I have also found myself needing to pass a varied number of variables to a function that can accept more or other variables if needed.
How do I pass many arguments into a function that is able to take a varied number of arguments ?
I have tried to create an example that is as basic as possible. The functions just perform some arithmetic operations on some variables, and then prints them out.
a = 10
b = 20
c = 30
def firstFunction(*args):
d = a *2
e = b /2
f = c +2
x = d -10
y = e -10
z = f -10
h = 1 #or 2
secondFunction(d,e,f,h)
thirdFunction(x,y,z,h)
def secondFunction(d,e,f,h):
if h == 1:
print d
print e
print f
def thirdFunction(x,y,z,h):
if h == 2:
print x
print y
print z
firstFunction(b,c,a)
And the results produced, as expected, for h=1 and h=2 respectively, are:
20
10
32
10
0
22
Now lets say I want to combine the second and third functions together, so I need to just call one function instead of two. The function would be, in this case:
def combinedFunction(d,e,f,h,x,y,z):
if h == 1:
print d
print e
print f
if h == 2:
print x
print y
print z
and would be called by: combinedFunction(d,e,f,h,x,y,z). As you can imagine, this can get extremely annoying for much more complicated functions. Also I am passing many different arguments that are not going to be used at all, and each of them has to be first declared. For example, in the example, if h = 1, x,y and z have to still be passed into the function, and maybe the value of one of them hasn't been determined as yet(in this simple case it is). I can't use 'combinedFunction(*args)' because not every argument is globally defined.
TLDR:
Basically I want the following:
def someFunction(accepts any number of arguments **and** in any order):
# does some stuff using those arguments it received at the time it was called
# it can accept many more parameters if needed
# it won't need to do stuff to a variable that hasn't been passed through
and this function is called by:
someFunction(sends any number of arguments **and** in any order)
# can call the function again using different arguments and a
# different number of arguments if needed
Can this be easily achieved?
def someFunction(**kwargs)does not solve the problem?def someFunction(**kwargs), won't all the arguments still need to be global? In the example,infirstFunction, I am creating new variables, and within that function I am calling an additional function, such ascombinedFunction. If I passed the variables the way I am currently doing it in the example it will work, but if I use kwargs (or args) then the variables need to be globally defined for it to work. I of course might be mistaken, but it didn't work when I tried it.combinedFunction(d,e,f,h,x,y,z), where if it is called, then " x,y and z have to still be passed into the function" in the case that "h =1" for example. I am saying I do not want this to happen.