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

  • Home
  • SEARCH
  • 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 8247305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:48:10+00:00 2026-06-07T22:48:10+00:00

I have a Python script which creates a dictionary of its own functions and

  • 0

I have a Python script which creates a dictionary of its own functions and I’d like it to execute them by reading in a function name and arguments using YARP (knowledge of YARP is irrelevant to this question though).

I create a list of strings called “inc” which is populated by values coming into the program. The first item is a function name, and any other strings in the list are arguments. I create a dictionary called “methods” where the key is the function name and the value is a reference to the function object (using the inspect module). I store the return value of the function in a variable “result”.

The snippet below shows a simplified version of what I’m using so far, which works fine, but can’t handle functions with more than one argument. To circumvent this I use a list if a function needs more parameters:

if len(inc) == 1:
    result = methods[inc[0]]()   # call method with 0 arguments

elif len(inc) == 2:
    result = methods[inc[0]](inc[1])    # call method passing a string

else:
    args = []
    result = methods(inc[0])(inc[1:])    # call method passing a list

Ideally, I’d like to change this so that my functions can have any number of arguments, but I can’t figure out how I can do this. I’m new to Python and I have looked at the documentation and various websites – I just can’t find a solution. I’ve tried things like creating a tuple of the arguments, but that doesn’t work either as it ends up passing the whole tuple in as one parameter.

Is there a better solution to this problem, like creating some kind of object which represents a set of parameters and passing that into the function? Any suggestions would be greatly appreciated!

  • 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-06-07T22:48:11+00:00Added an answer on June 7, 2026 at 10:48 pm

    You should check out https://stackoverflow.com/a/3394898/1395668.

    The magic you are looking for is the *. Apply this to your list and it unpacks the items into the argument fields of your function:

    a = [ 1, 2, 3]
    def myfunc(a, b, c):
        return a + b + c
    
    print myfunc(*a)
    

    Check out ** for the same approach for dict

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

Sidebar

Related Questions

I have the following python script which takes some inputs and puts them in
I have python script which downloads N number of images from website. I run
I have a python script which outputs lots of data, sample is as below.
i have a python script which keeps crashing on: subprocess.call([pdftotext, pdf_filename]) the error being:
I have a small python script which draws some turtle graphics. When my script
I have written a python script which watches a directory for new subdirectories, and
In my Python script which uses Curses, I have a subwin to which some
I have a python script using the boto library on ec2 instance which is
I have a python script that runs a program, which generates few .exe files
I have a list which I have obtained from a python script. the content

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.