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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:37:14+00:00 2026-05-27T20:37:14+00:00

I know it’s not Pythonic to write functions that care about the type of

  • 0

I know it’s not Pythonic to write functions that care about the type of the arguments, but there are cases when it’s simply impossible to ignore types because they are handled differently.

Having a bunch of isinstance checks in your function is just ugly; is there any function decorator available that enables function overloads? Something like this:

@overload(str)
def func(val):
    print('This is a string')

@overload(int)
def func(val):
    print('This is an int')

Update:

Here’s some comments I left on David Zaslavsky’s answer:

With a few modification[s], this will suit my purposes pretty well. One other limitation I noticed in your implementation, since you use func.__name__ as the dictionary key, you are prone to name collisions between modules, which is not always desirable. [cont’d]

[cont.] For example, if I have one module that overloads func, and another completely unrelated module that also overloads func, these overloads will collide because the function dispatch dict is global. That dict should be made local to the module, somehow. And not only that, it should also support some kind of ‘inheritance’. [cont’d]

[cont.] By ‘inheritance’ I mean this: say I have a module first with some overloads. Then two more modules that are unrelated but each import first; both of these modules add new overloads to the already existing ones that they just imported. These two modules should be able to use the overloads in first, but the new ones that they just added should not collide with each other between modules. (This is actually pretty hard to do right, now that I think about it.)

Some of these problems could possibly be solved by changing the decorator syntax a little bit:

first.py

@overload(str, str)
def concatenate(a, b):
    return a + b

@concatenate.overload(int, int)
def concatenate(a, b):
    return str(a) + str(b)

second.py

from first import concatenate

@concatenate.overload(float, str)
def concatenate(a, b):
    return str(a) + b
  • 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-27T20:37:15+00:00Added an answer on May 27, 2026 at 8:37 pm

    Quick answer: there is an overload package on PyPI which implements this more robustly than what I describe below, although using a slightly different syntax. It’s declared to work only with Python 3 but it looks like only slight modifications (if any, I haven’t tried) would be needed to make it work with Python 2.


    Long answer: In languages where you can overload functions, the name of a function is (either literally or effectively) augmented by information about its type signature, both when the function is defined and when it is called. When a compiler or interpreter looks up the function definition, then, it uses both the declared name and the types of the parameters to resolve which function to access. So the logical way to implement overloading in Python is to implement a wrapper that uses both the declared name and the parameter types to resolve the function.

    Here’s a simple implementation:

    from collections import defaultdict
    
    def determine_types(args, kwargs):
        return tuple([type(a) for a in args]), \
               tuple([(k, type(v)) for k,v in kwargs.iteritems()])
    
    function_table = defaultdict(dict)
    def overload(arg_types=(), kwarg_types=()):
        def wrap(func):
            named_func = function_table[func.__name__]
            named_func[arg_types, kwarg_types] = func
            def call_function_by_signature(*args, **kwargs):
                return named_func[determine_types(args, kwargs)](*args, **kwargs)
            return call_function_by_signature
        return wrap
    

    overload should be called with two optional arguments, a tuple representing the types of all positional arguments and a tuple of tuples representing the name-type mappings of all keyword arguments. Here’s a usage example:

    >>> @overload((str, int))
    ... def f(a, b):
    ...     return a * b
    
    >>> @overload((int, int))
    ... def f(a, b):
    ...     return a + b
    
    >>> print f('a', 2)
    aa
    >>> print f(4, 2)
    6
    
    >>> @overload((str,), (('foo', int), ('bar', float)))
    ... def g(a, foo, bar):
    ...     return foo*a + str(bar)
    
    >>> @overload((str,), (('foo', float), ('bar', float)))
    ... def g(a, foo, bar):
    ...     return a + str(foo*bar)
    
    >>> print g('a', foo=7, bar=4.4)
    aaaaaaa4.4
    >>> print g('b', foo=7., bar=4.4)
    b30.8
    

    Shortcomings of this include

    • It doesn’t actually check that the function the decorator is applied to is even compatible with the arguments given to the decorator. You could write

      @overload((str, int))
      def h():
          return 0
      

      and you’d get an error when the function was called.

    • It doesn’t gracefully handle the case where no overloaded version exists corresponding to the types of the arguments passed (it would help to raise a more descriptive error)

    • It distinguishes between named and positional arguments, so something like

      g('a', 7, bar=4.4)
      

      doesn’t work.

    • There are a lot of nested parentheses involved in using this, as in the definitions for g.
    • As mentioned in the comments, this doesn’t deal with functions having the same name in different modules.

    All of these could be remedied with enough fiddling, I think. In particular, the issue of name collisions is easily resolved by storing the dispatch table as an attribute of the function returned from the decorator. But as I said, this is just a simple example to demonstrate the basics of how to do it.

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

Sidebar

Related Questions

I know that there's something fishy about the malloc part, but I'm having trouble
I know very little about Agile but I wonder if there is any difference
I know that this may be common knowledge, but is there a way to
Know anybody something about hooking __userpurge type of functions? I hooking successfully __thiscall ,
know nothing about php, but I have this script that reads a folder and
I know that lot of questions about HTML sanitizers have appeared in SO, but
Know anybody something about hooking __usercall type of functions? I hooking successfully __thiscall ,
I know similar questions have been asked, but I'm not sure about the answers
I know that design patterns is generally something that's connected to OO programming, but
I know there are similar questions already on SO but none of them seem

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.