Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:45:16+00:00 2026-05-31T02:45:16+00:00

I find myself in a need of working with functions and objects who take

  • 0

I find myself in a need of working with functions and objects who take a large number of variables.

For a specific case, consider a function from a separated module which takes N different variables, which are then pass them on to newly instanced object:

def Function(Variables): 
    Do something with some of the variables
    object1 = someobject(some of the variables)
    object2 = anotherobject(some of the variables, not necessarily as in object1)

While i can just pass a long list of variables, from time to time i find myself making changes to one function, which requires making changes in other functions it might call, or objects it might create. Sometimes the list of variables might change a little.

Is there a nice elegant way to pass a large group of variables and maintain flexibility?

I tried using kwargs in the following way:

def Function(**kwargs):

    Rest of the function

and calling Function(**somedict), where somedict is a dictionary has keys and values of all the variables i need to pass to Function (and maybe some more). But i get an error about undefined global variables.

Edit1:

I will post the piece of code later since i am not at home or the lab now. Till then i will try to better explain the situation.

I have a molecular dynamics simulation, which take few dozens of parameters. Few of the parameters (like the temperature for example) need to be iterated over. To make good use of the quad core processor i ran different iterations in parallel. So the code starts with a loop over the different iteration, and at each pass send that parameters of that iteration to a pool of workers (using the multiprocessing module). It goes something like:

P = mp.pool(number of workers) # If i remember correctly this line 
for iteration in Iterations:
     assign values to parameters
     P.apply_async(run,(list of parameters),callback = some post processing)
P.close()
P.join()

The function run takes the list of parameters and generates the simulation objects, each take some of the parameters as their attributes.

Edit2:

Here is a version of the problematic function. **kwargs contain all the parameters needed by the ‘sim’,’lattice’ and ‘adatom’.

def run(**kwargs): 
"""'run' runs a single simulation process.
j is the index number of the simulation run.
The code generates an independent random seed for the 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 is 1:
    # If the simulation did not complete successfuly, run it again.
    bad = sim.timeloop(lattice,adatom1,j)
print 'Starting post processing'
# Return the temperature and adatomś trajectory and velocity
List = [j,lattice.temp , adatom1.traj ,adatom1.velocity, lattice.Toptemp,     lattice.Bottomtemp, lattice.middletemp, lattice.latticetop]
return  List        
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:45:17+00:00Added an answer on May 31, 2026 at 2:45 am

    The cleanest solution is not using that many parameters in a function at all.

    You could use set methods or properties to set each variable separately, storing them as class members and being used by the functions inside that class.

    Those functions fill the private variables and the get methods can be used to retrieve those variables.

    An alternative is to use structures (or classes without functions) and this way create a named group of variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm teaching myself to program in Haskell, and I'm working on a find function.
am working on php loops and i find myself that i need to write
i am working with away3D and i find myself in dire need of a
I find myself with a need for a java utility for taking a fully-qualified
I find myself doing this repeatedy. $jq(button).filter(function(){ return this.id.match(/^user_(\d+)_edit$/); }).click(function(){ var matches = this.id.match(/^user_(\d+)_edit$/);
I find myself declaring bunch of class variables and it is really tiring to
I often find myself in need of writing down a future Visual Studio solution
I just ran into a conflict while working in SVN, and I find myself
I often find myself in need of very complex SQL examples when testing abstraction
I find myself currently working on several projects which include user entries in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.