1

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?

4
  • 3
    Could you please clarify why def someFunction(**kwargs) does not solve the problem? Commented Jun 20, 2018 at 10:26
  • 1
    I often find myself needing to pass a plethora of arguments into a function - That's a code smell; I am passing many different arguments that are not going to be used at all - Then why do you pass them? I think you should reconsider your design. Commented Jun 20, 2018 at 10:42
  • @Eugene Primako, with def someFunction(**kwargs) , won't all the arguments still need to be global? In the example,in firstFunction, I am creating new variables, and within that function I am calling an additional function, such as combinedFunction. 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. Commented Jun 20, 2018 at 11:33
  • @ mkrieger1, the statement "I am passing many different arguments that are not going to be used at all" refers to the use of 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. Commented Jun 20, 2018 at 11:39

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I didn't know about kwargs.get(). It's still a bit messy but it does what I wanted it to do.
To clarify - kwargs is an usual dict, so you can use kwargs.get('a'), kwargs['a'] and all the other dict operations.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.