Handling and passing large group of variables to functions\objects in python -


i find myself in need of working functions , objects take large number of variables.

for specific case, consider function separated module takes n different variables, pass them on newly instanced object:

def function(variables):      of variables     object1 = someobject(some of variables)     object2 = anotherobject(some of variables, not in object1) 

while can pass long list of variables, time time find myself making changes 1 function, requires making changes in other functions might call, or objects might create. list of variables might change little.

is there nice elegant way pass large group of variables , maintain flexibility?

i tried using kwargs in following way:

def function(**kwargs):      rest of function 

and calling function(**somedict), somedict dictionary has keys , values of variables need pass function (and maybe more). error undefined global variables.

edit1:

i post piece of code later since not @ home or lab now. till try better explain situation.

i have molecular dynamics simulation, take few dozens of parameters. few of parameters (like temperature example) need iterated over. make use of quad core processor ran different iterations in parallel. code starts loop on different iteration, , @ each pass send parameters of iteration pool of workers (using multiprocessing module). goes like:

p = mp.pool(number of workers) # if remember correctly line  iteration in iterations:      assign values parameters      p.apply_async(run,(list of parameters),callback = post processing) p.close() p.join() 

the function run takes list of parameters , generates simulation objects, each take of parameters attributes.

edit2:

here version of problematic function. **kwargs contain parameters needed 'sim','lattice' , 'adatom'.

def run(**kwargs):  """'run' runs single simulation process. j index number of simulation run. code generates independent random seed initial conditios.""" scipy.random.seed() sim = mdf.simulation(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat,savetemp) lattice = mdf.lattice(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, kb, ks, kbs, a, p, q, massl, randinit, initvel, parangle,scaletemp,savetemp,freeze) adatom = mdf.adatom(tstep, temp, time, writeout, boundaryxy, boundaryz, relax, insert, lat, ra, massa, amorse, bmorse, r0, z0, name, lattice, samplerate,savetemp,adatomrelax)                  bad = 1  print 'starting simulation run number %g\nrun' % (j+1)     while bad 1:     # if simulation did not complete successfuly, run again.     bad = sim.timeloop(lattice,adatom1,j) print 'starting post processing' # return temperature , adatomÅ› trajectory , velocity list = [j,lattice.temp , adatom1.traj ,adatom1.velocity, lattice.toptemp,     lattice.bottomtemp, lattice.middletemp, lattice.latticetop] return  list         

the cleanest solution not using many parameters in function @ all.

you use set methods or properties set each variable separately, storing them class members , being used functions inside class.

those functions fill private variables , methods can used retrieve variables.

an alternative use structures (or classes without functions) , way create named group of variables.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -