64

As function overloading says:

Function overloading is absent in Python.

As far as I feel this a big handicap since its also an object-oriented (OO) language. Initially I found that unable to differentiate between the argument types was difficult, but the dynamic nature of Python made it easy (e.g. list, tuples, strings are much similar).

However, counting the number of arguments passed and then doing the job is like an overkill.

Any pythonic way to do this?

Xolve
  • 22,298
  • 21
  • 77
  • 125
  • 3
    So? What do you think you need overloading for? – kquinn Apr 09 '09 at 07:59
  • 2
    Voting to close as not a real question. That's not to say it's not a potentially valid point, but it's not a question. – Jon Skeet Apr 09 '09 at 08:01
  • Well, then use a language that provides overloading... – Ionuț G. Stan Apr 09 '09 at 09:39
  • 2
    Also voting to close. This is either an implied question or a rant, either of which is hard to answer. – Jarret Hardie Apr 09 '09 at 12:05
  • 48
    It could have been a question if I just added -- "any Pythonic way to do this?". However I feel this is implied in my post. – Xolve Apr 09 '09 at 13:59
  • 1
    Guido has actually provided a pythonic solution to overloading http://www.artima.com/weblogs/viewpost.jsp?thread=155514 Though other solutions exist, like the following http://www.ibm.com/developerworks/library/l-pydisp.html I think Peak or something similar had a fairly complex one. – Ed. Apr 09 '09 at 19:28
  • 4
    @Xolve Explicit is better than implicit. – kon psych Jan 28 '15 at 21:05
  • @konpsych oh my god are you kidding me? This is EXACTLY why people make fun of programmers and Stack Overflow. Human conversation isn't a programming language! Jesus Christ. – Dan Jun 22 '18 at 16:23
  • Related (not duplicate): *[How can I detect duplicate method names in a Python class?](https://stackoverflow.com/questions/10761988)* – Peter Mortensen Jan 06 '21 at 16:19

5 Answers5

36

Now, unless you're trying to write C++ code using Python syntax, what would you need overloading for?

I think it's exactly opposite. Overloading is only necessary to make strongly-typed languages act more like Python. In Python you have keyword argument, and you have *args and **kwargs.

See for example: What is a clean, Pythonic way to have multiple constructors in Python?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vartec
  • 131,205
  • 36
  • 218
  • 244
  • 62
    Your assessment that overloading is _"hack-work for handicapped languages"_ seems overly polarised and overlooks the benefit of static typing, and its suitability to a different type of deployment. [Stefano Borini's answer](http://stackoverflow.com/a/733385/322333) that _"..function overloading is based on the idea that passing different types you will execute different code. If you have a dynamically typed language like python, you should not distinguish by type"_ is more informative and helpful I think. – boycy Feb 05 '14 at 15:30
  • 11
    Of course, you can use variable arguments in C++, too, yet overloading is used to keep code *clean*. As in, one function does one thing rather than having a single function littered with if statements trying to figure out what to do based on what was passed in. – Nerdmaster Feb 13 '14 at 19:00
33

As unwind noted, keyword arguments with default values can go a long way.

I'll also state that in my opinion, it goes against the spirit of Python to worry a lot about what types are passed into methods. In Python, I think it's more accepted to use duck typing -- asking what an object can do, rather than what it is.

Thus, if your method may accept a string or a tuple, you might do something like this:

def print_names(names):
    """Takes a space-delimited string or an iterable"""
    try:
        for name in names.split(): # string case
            print name
    except AttributeError:
        for name in names:
            print name

Then you could do either of these:

print_names("Ryan Billy")
print_names(("Ryan", "Billy"))

Although an API like that sometimes indicates a design problem.

Ryan Ginstrom
  • 13,915
  • 5
  • 45
  • 60
21

You don't need function overloading, as you have the *args and **kwargs arguments.

The fact is that function overloading is based on the idea that passing different types you will execute different code. If you have a dynamically typed language like Python, you should not distinguish by type, but you should deal with interfaces and their compliance with the code you write.

For example, if you have code that can handle either an integer, or a list of integers, you can try iterating on it and if you are not able to, then you assume it's an integer and go forward. Of course it could be a float, but as far as the behavior is concerned, if a float and an int appear to be the same, then they can be interchanged.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 8
    Overloading isn't strictly about types, it's also about number of arguments. For instance, I may want my accessor and mutator (or "getter" and "setter" if you prefer) to share a single method name, but they are clearly doing separate things based on the presence of an argument. This has nothing to do with the type of argument. – Nerdmaster Feb 13 '14 at 19:02
6

Oftentimes you see the suggestion use use keyword arguments, with default values, instead. Look into that.

unwind
  • 391,730
  • 64
  • 469
  • 606
6

You can pass a mutable container datatype into a function, and it can contain anything you want.

If you need a different functionality, name the functions differently, or if you need the same interface, just write an interface function (or method) that calls the functions appropriately based on the data received.

It took a while to me to get adjusted to this coming from Java, but it really isn't a "big handicap".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lprsd
  • 84,407
  • 47
  • 135
  • 168
  • hi @lprsd! can you provide a simple example of interface function. ofcourse it _seems_ trivial enough, but i want to see how it's done by those who know to ensure i won't miss anything crucial while DIY-ing. thanks :) – user8395964 Jun 12 '23 at 12:24