1

Given that

Explicit is better than implicit.

But also

Simple is better than complex.

I was wondering how accepted it is to transform code like this:

import argparse

def func(a, b):
    return a + b

parser = argparse.ArgumentParser()
parser.add_argument('a')
parser.add_argument('b')
parser.add_argument('c')

args = parser.parse_args()
func(args.a, args.b)

Into code like this:

import argparse

def func(a, b, **kwargs):
    return a + b

parser = argparse.ArgumentParser()
parser.add_argument('a')
parser.add_argument('b')
parser.add_argument('c')

args = vars(parser.parse_args())
func(**args)

In order to avoid always having to write args.a, args.b, args.c. Obviously this becomes only relevant when many options are involved, more complicated functions and longer variable names.

0
1

I'd say it's very acceptable. It's very easy to visualise what end goal you're trying to achieve with the code. Whenever you call add_argument() and look at the signature of the function you're calling, you can already see the arguments, thus reusing their name again when calling func() seems redundant indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.