user3451660
user3451660

Reputation: 477

Passing mulitple and varied function arguments in Python

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?

Upvotes: 1

Views: 114

Answers (3)

Eugene Primako
Eugene Primako

Reputation: 2817

Using global variables from inside a function is generally a bad approach. Instead of it you can use **kwargs this way:

def firstFunction(**kwargs):
    d = kwargs.get('a') * 2

    secondFunction(d=d, **kwargs)
    thirdFunction(e=1, **kwargs)

def secondFunction(d, **kwargs):
    print d
    print kwargs.get('a')

def thirdFunction(**kwargs):
    print kwargs.get('e')

firstFunction(a=1, b=3, q=42)  # q will not be used

Upvotes: 2

Rohi
Rohi

Reputation: 814

You could use a dict to pass the data to the functions, but it does makes it less intuitive when programming. Each function could the convert the dict arguments as you please.

def func_a(input_dict):
    a = input_dict["a"]
    b = input_dict["b"]
    print(a+b)

def func_b(input_dict):
    c = input_dict["c"]
    d = input_dict["d"]
    print(c+d)

def combined_func(input_dict):
    func_a(input_dict)
    func_b(input_dict)

This is quite similar to kwargs, so it might not be what you are looking for.

Upvotes: 1

J. Snow
J. Snow

Reputation: 306

If I understood correctly what you are looking for:

def something(*args):
    for i in args:
        print(i)

some_args = range(10)

something(*some_args)
print('-----------')
something(*some_args[::-1]) # reverse order

Output:

0
1
2
3
4
5
6
7
8
9
-----------
9
8
7
6
5
4
3
2
1
0

Upvotes: 0

Related Questions