Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a function that calls on several other functions with default arguments. I have been explicitly passing every parameter of the inner functions to the outer function but this has caused the outer function parameter list to become very long. Is there a common way to handle this? Below is an example of what I have been doing and am looking for what is common practice.
Python
class my_class:
    def inner1(self, arg1=1, arg2=2):
        return (arg1 + arg2)

    def inner2(self, arg3=3, arg4=4):
        return (arg3 * arg4)

    def outer(self, arg5, arg6, arg1=1, arg2=2, arg3=3, arg4=4):
        x1 = self.inner1(arg1, arg2)
        x2 = self.inner2(arg3, arg4)
        x3 = x1 + x2 + arg5 - arg6
        return x3

I primarily run the outer function and once in a while I might want to change the input parameters arg1, arg2, arg3, and/or arg4. I don't want to keep such a long list of input parameters to outer. What can be done here? Please provide an example as I am new to programming. Thank you!

What I have tried:

I have tried using __init__ to instantiate the variables but I do not want to modify the object every time I want to change arg1 for example.

I have looked into *args and **kwargs but because these are required function inputs, I am not sure this would work.
Posted
Comments
Richard MacCutchan 24-Oct-17 4:11am    
I have looked into *args and **kwargs
And that is really the only way to do it with long lists.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900