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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:06:42+00:00 2026-05-24T01:06:42+00:00

I have a function that is taking an arbitrary set of arguments and then

  • 0

I have a function that is taking an arbitrary set of arguments and then choosing the correct function to process them based on the types of the arguments.

My current approach is using a decorator on all of the processing functions that checks the types of the arguments, and then iterating through all of the functions until one accepts the arguments.

Something about this seems a little hacky to me, and as a relatively new python programmer, I was wondering if there was a more ‘pythonic’ method to doing this.

so, currently, I have something like this:

def function_router(*args):
   for func in functions: #functions is a list of functions
      try:
         return func(*args)
      except TypeError:
         pass
    #probably raise an exception if no function works

and each function in ‘functions’ would have a decorator like this:

def accepts(*types) :
   def my_decorator(func):
      def wrapped(*args, **kwargs):
         for i in range(len(types)):
            if not isinstance(args[i], types[i]):
               raise TypeError('Type error, %s not instance of %s, it is %s' %(args[i],types[i], type(args[i])))
            return func(*args, **kwargs)
      return wrapped
   return my_decorator

EDIT: Oh man, I actually really liked reading all of the solutions. The answer I chose was most effective for what I’m currently doing, but I learned something from all of the answers, so thank you all for your time.

  • 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-24T01:06:44+00:00Added an answer on May 24, 2026 at 1:06 am

    Perhaps the right way to do this is to use keyword arguments, instead of relying on the types of the arguments. That way, you don’t have to decorate the little functions, just name the arguments correctly. It will also let you take advantage of Python’s duck typing.

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

Sidebar

Related Questions

I have a function that automatically exports a table into a CSV file, then
I have a function that's taking a long time to run. When I profile
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that I use called sqlf(), it emulates prepared statements. For
I have a function that is effectively a replacement for print, and I want
I have a function that takes a struct, and I'm trying to store its
I have a function that checks if a cookie (by name) exists or not:

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.