I'm working on a package that contains subpackages and several modules within each subpackage. Most of the functions need several arguments (~10) that are initiated in the "main" function. Usually, all the arguments are passed to methods that are called from within a function. What is the best way to pass around these parameters ?
Consider the following scenario:
def func(arg1, arg2, ...., argn):
do something
ext_method(arg1, arg3,...argk) # Note: all the arguments are not passed to ext_method
cleanup
PS: Function nesting can go quite deep (say around 10-12 functions starting from main function). I considered two alternatives:
Inspect method: Construct a dict of all the arguments in the main function with all the arguments. To call a function: Get its argspec using the method in inspect package, and filter out unnecessary arguments from the "global" arguments dict. It looks something like the below snippet.
args = {arg1: obj1, arg2:obj2,...} req_args = dict([(k, v) for k, v in args.items() if k in inspect.getargspec(ext_method).args])
key-value method:
ext_method()
is called using argument key-value pairsext_method(arg1=val1, arg3=val3,...,argk=valk)
I find the second method to be ineffective because its not easy to change the signature of ext_method()
as more features are added to the package. Is method 1 a better alternative ?