python - Best way to call default keyword arguments -


i hoping way make passing arguments function easy. making huge amount of functions ton of variables taken each one. each function calls lot of functions underneath them, use of same parameters.

i have decided can create structure contain default parameters these functions , user may set them constant @ value decide or allowed vary fitting procedure performed - however, default value given up-front each parameter.

i have been thinking of best way functions function f() called take amount of relevant parameters; f() return same f(default_param_structure), defaults assumed. in addition, calling f(arg1=1, arg31='a') replace relevant parameters.

here example trying work out:

import pandas pd import numpy np default_a = 1 default_a_min = 0 default_a_max = 2 default_b = 2 default_b_min = 1 default_b_max = 3  def default_param_struct():     = np.array([default_a, default_a_min, default_a_max])     b = np.array([default_b, default_b_min, default_b_max])     d = {'a': a, 'b': b}     return pd.dataframe(d, index=['val', 'min','max'])  def f(a=default_a, b=default_b, *args, **kwargs):     return kwargs  default_param_df = default_param_struct() print default_param_df def_param_dict = default_param_df.loc['val'].to_dict() print def_param_dict   # should print {'a': 3, 'b': 2}  (i.e. passed , default_b given automatically) print f({'a':3}) # should print {'a': 1, 'b': 2}  (as default parameter structure calculated in function) print f(def_param_dict) # should print {'a': 1, 'b': 2}  (default parameter structure assumed) print f() # should print {'a': 4, 'b': 2}  (i.e. passed , default_b given automatically) print f(a=4) # should print {'a': 3, 'b': 5}  both passed print f(a=3, b=5) 

but output is:

      b val  1  2 min  0  1 max  2  3 {'a': 1, 'b': 2} {} {} {} {} 

so none of arguments making in. know how solve this? there more elegant solution?

you write

i making huge amount of functions ton of variables taken each one.

there technical solutions this, indication of design flaw.

e.g., if have functions calling each other, passing same huge number of arguments on , over, perhaps should methods of class, , of arguments should members. both increase encapsulation decrease number of arguments passed (the arguments implicit in self).

otherwise, might consider making parameters oo hierarchy. perhaps might need class describing parameters; perhaps needs subclassed, etc.

imho, shouldn't solving technical tricks.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -