Overloading in Python

Some say Python doesn’t allow overloading of methods or constructors. Well, they are right in some way!  A coding example below,

[sourcecode language=”python”]
def add(a,b):
return a+b

def add(a,b,c):
return a+b+c

print add(4,5)

If you try to run the above piece of code, you get an error stating, “TypeError: add() takes exactly 3 arguments (2 given)”. This is because, Python understands the latest definition of method add() which takes only two arguments. Even though a method add() that takes care of three arguments exists, it didn’t get called. Hence you would be safe to say, overloading methods in Python is not supported.

But, then there are folks who are more than willing to say, ‘Oh! Python supports all!’ Yes, Python supports overloading but in a Pythonic way. Here’s an example,

[sourcecode language=”python”]
def add(instanceOf, *args):
if instanceOf == ‘int’:
result = 0
if instanceOf == ‘str’:
result = ”
for i in args:
result = result + i
return result

print add(‘int’, 3,4,5)
print add(‘str’, ‘I’,’ am’,’ in’, ‘ Python’)

I am in Python


In the above code snippet, two things are achieved:

–          Irrespective of the different number of arguments, method add() works well

–          Also, based on the data type of input, data  type of output is changed

So, overloading IS there in Python!! 🙂

5 thoughts on “Overloading in Python

  1. You could have used : instanceOf as type_
    if type_ is int:
    result = type()
    that would simply be a rewrite of the `sum` function 🙂
    Usually that means you have an issue in your code, it’s hard to guess what function is really called when you define many of them with the same name.
    Hi you may also use this which does auto guessing for types code for fun or some scripting only.
    I do not recommend the use of this for other things that simple tooling as the function output and behavior is not commutative.
    # coding: utf-8
    def adder(*args):
    t = type(args[0])
    tnill = t()
    for arg in args:
    tnill += arg
    tnill += t(arg)
    return tnill
    adder(‘a’, ‘b’,’c’)
    adder(‘a’, 5, ‘d’)
    adder(5, 5, ‘d’) # raises typerror.
    adder(‘d’, 5, 5) # works.
    adder(5, 5, 23)
    adder(‘5’, 5)
    adder(5, ‘5’)
    def adder(*args):
    default_type = type(args[0])
    sum_ = default_type()
    for arg in args:
    sum_ += arg
    sum_ += default_type(arg)
    return sum_
    And this is why you should not use it:
    In [22]: adder(‘d’, [5, 5, None], ‘fooo’)
    Out[22]: ‘d[5, 5, None]fooo’
    Because you can turn anything to a string.
    There are some modern tricks you can use like ‘simple dispatch’ https://docs.python.org/3.6/library/functools.html#functools.singledispatch
    For much more fun and busywork you may try to cast sum_ to the type of current arg… when raising error on sum_ += arg or cast arg to the type of sum_ seems cool isn’t ?
    Luckily int and float can be added seamlessly, but …
    give a try
    adder(‘5’, 5, ‘5’) -> ‘555’ or 15 or ‘105’ ? 105 (add 5 to ‘5’ then cast and append the string ‘5’)
    adder(5, ‘5’, ‘5’) -> 15 ? or 555 ?
    adder(‘5’, ‘5’, 5) – > 60 or 15 or 555?
    with such strategies cast as type(arg)(sum) + arg or type(sum)(arg) + sum
    And that is why i dont think that this should not be used for “production” code or without thoroughly checking your input first.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.