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 3854612
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:38:59+00:00 2026-05-19T17:38:59+00:00

Suppose I have, class example(object) def __init__(var1 = 10, var2 = 15, var3 =

  • 0

Suppose I have,

class example(object)
    def __init__(var1 = 10, var2 = 15, var3 = 5)
        do a few things here 

    Other methods here

There are other classes which are not relevant for the question.

To study the behavior of the system, I vary the inputs in the __init__ method above, one at a time. I have another function start_simulation function that takes the name of the input that I want to change as a string and its possible values. (It uses this name and the values to also create file names that store the results from the execution). For example, I have

def start_simulation(var_under_study, var_under_study_values):
    '''type of var_under_study: string''' 
    for value in var_under_study_values:

        example_instance = example(var_under_study = value) 
        # I have to specify the var_under_study manually in this line. 
        # If it is var1, I have to manually type var1 here. 
        # I want to know if there is a way so that once I specify 
        # the string var_under_study in the argument of the 
        # start_simulation function, this line takes that and sets the
        # variable with that name to the specified value.

        other stuff here

I call this function elsewhere by writing

start_simulation(var_under_study = 'var1', var_under_study_values = [5, 15, 20])

Now, if I want to study effects of var2 instead, I have to specify it in the arguments of start_simulation function:

start_simulation(var_under_study = 'var2', var_under_study_values = [5, 15, 20])

but I also have to go back where the function is defined and change the argument in the line example_instance = example(var_under_study = value). For instance, in place of example_instance = example(var1 = value) I have to write:

example_instance = example(var2 = value)

Is there a way I can just specify the variable in the

start_simulation(var_under_study = 'var2', var_under_study_values = [5, 15, 20])

and have

example_instance = example(var1 = value)

line take account of that.

Thanks for your help. Please let me know if I can clarify. I am trying to avoid having to change the same/similar thing in multiple places so that I don’t get incorrect results because I forgot to change it somewhere.

  • 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-19T17:38:59+00:00Added an answer on May 19, 2026 at 5:38 pm

    If I understand you correctly, you want to specify the name of the parameter that should be set dynamically. You can use dictionary unpacking:

    example_instance = example(**{var_under_study: value})
    

    This will pass the contents of the dictionary as parameters to the function, where the keys are the parameter names and the values the values 🙂

    Side note: You should start class names with an uppercase letter to make them more distinguishable from a function.

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

Sidebar

Related Questions

No related questions found

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.