Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid passing too many arguments

Tags:

python

I have the following code where I need to pass all the arguments from one function to another. I want to know a way avoid the long list of arguments. I only "know" there is "*" and "**" in python, but I have never used them before.

# definition
    def TestCase(test_name, op_type, input_shapes, op_args, run_mode):
        # all those arguments are unchanged before passing to 
        # "add_tester"
         ...

        # another function, the long list of arguments doesn't look 
        # good to me  
        add_tester("c2", test_name, input_shapes, op_args, run_mode, benchmark_func)

# Call TestCase
TestCase(
test_name='mm',
op_type='MM',
input_shapes=input_shapes,
op_args={'trans_a': trans_a, 'trans_b': trans_b},
run_mode=run_mode)
like image 744
Zack Avatar asked Jun 18 '26 19:06

Zack


2 Answers

Write a class, put in the parameters at __init__, and use self.

class TestCase:
    def __init__(self, test_name, op_type, run_mode, benchmark_func):
        self._test_name = test_name
        self._op_type = op_type
        self._run_mode = run_mode
        self._benchmark_func = benchmark_func
        # bunch of initiation code follows

    # another function, the long list of arguments doesn't look 
    # good to me  
    def run_test(self, op_args, idk_what_this_is="c2"):
        # access self._XX for the fields

A few notes:

  1. Be careful with naming conventions. Use lowercase with underscore for functions/methods.
  2. If you are doing conventional testing, consider existing frameworks like nose. There are lots of code pattern that you don't need to rewrite by doing so.
like image 192
Pik-Mai Hui Avatar answered Jun 20 '26 09:06

Pik-Mai Hui


If you are interested about how to use * and ** notations look at the example:

def f(a, *args, **kwargs):
     print("a: %s, args: %s, kwargs: %s" % (a,args, kwargs))

f(1, 2, 3, 4, b=5, c=6, d=7)
# output: A: 1, args: (2, 3, 4), kwargs: {'b': 5, 'c': 6, 'd': 7}

What does ** (double star/asterisk) and * (star/asterisk) do for parameters? contains a good explanation on this things.

As for your code if you don't want to pass all your arguments one by one, you can use **kwargs for all your variables:

# definition
def TestCase(**kwargs):
    # all those arguments are unchanged before passing to 
    # "add_tester"
     ...

    # another function, the long list of arguments doesn't look 
    # good to me  
    add_tester("c2", kwargs)

# Call TestCase
TestCase(test_name='mm', op_type='MM', input_shapes=input_shapes, op_args={'trans_a': trans_a, 'trans_b': trans_b}, run_mode=run_mode)

But you will need to refer to your arguments as kwargs['test_name'], kwargs['op_type'], ... and your arguments become optional, whoever calls your method will not see what actual arguments are expected.

like image 39
andnik Avatar answered Jun 20 '26 09:06

andnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!