Choosing a function based on parameter types in python
<p>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. </p>

<p>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.</p>

<p>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.</p>

<p>so, currently, I have something like this:</p>

<pre><code>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
</code></pre>

<p>and each function in 'functions' would have a decorator like this:</p>

<pre><code>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
</code></pre>

<p>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.</p>
