r/pythontips • u/Salaah01 • Jan 03 '23
Standard_Lib Turns out Python supports function overloading
I read up on a technique to overload Python functions. To my surprise, it's been available since Python 3.4!
"This must be something everyone knows that I just haven't heard about," I thought. I mentioned in my team retro that this is something I've recently learned and it turned out that actually, no one in my team heard of it!
And so, I decided to write an article and explain how it works as I suspect this might be something new to a lot of Pythonistas here: https://python.plainenglish.io/did-you-know-python-supports-function-overloading-6fa6c3434dd7
8
u/ra_wattt Jan 03 '23
Yes it does but at the same time it doesn't. The decorator singledispatch from functools has a limitation that it only works for the first argument of the function, but in other programming languages there is no such limitation. And I would suggest you to checkout overload function from typing library as well but afaik it also has some sort of limitations.
2
u/superbirra Jan 04 '23
The @overload-decorated definitions are for the benefit of the type checker only.
2
1
u/mudkripple Jul 01 '25
Ew gross. The first example with a tree of if-statements is better than the suggested decorator version, but both are still dumb, ugly, and would be difficult to maintain.
Unless your use case really needs it (which is fair, I can imagine some scenarios that might) just make differently-named functions for different parameters.
1
u/Salaah01 Jul 08 '25
lol it's an example in the article. It's not a suggestion!
Literally the end of the article says.. "so works in python... but consider the if/else or the match case"
1
u/zakmck73 Dec 26 '25
if you have display_int() and display_str(), then you can't obtain that x is passed to the right display_xxx() based on typeof(x). Not having to dispatch it statically/manually is the point of having polymorphism (ie, it helps a lot with the open-closed principle, which serious developers should know and apply).
1
u/mudkripple Dec 26 '25
I don't disagree, but like I said: only for certain use cases.
For something like a "display()" function, the specifics for handling each variable type need to be defined somewhere. Putting it in the function name or putting it in a giant if statement within the function is just a choice of how far down the road you want to kick that can.
Also, no, the "point" of polymorphism is so you don't have to manually allocate the memory, which is a major hassle in older languages. Not handling the variable types is a bonus you can choose when to use, but the developers of the language quickly added things like "type hinting" because they realized: The longer you wait to rigidly decide which data is flowing through which code, the more code will be prone to invisible errors and difficult to predict behaviors.
That's the trade off. Either you get softly typed variables like Python, or "softly typed" function names like C++. Something has to be hard typed somewhere, and personally I think "gigantic if statement" or "entire library with function decorators" are the worst options.
1
u/zakmck73 Dec 27 '25 edited Dec 27 '25
Putting it in the function name or putting it in a giant if statement within the function is just a choice of how far down the road you want to kick that can.
If you truly care about the open/closed, you don't do either. You either exploit the language polymorphism, or use a dictionary-based registry, or a factory.
And you do it because, in addition to separation of concerns, you want extendibility too.
You have a new ComplexNumber for display, your initial library had no idea that exists or how to display it, you can add the handler for it to the register of supported types, or, if you have polymorphism, just add display ( c: ComplexNumber ).
Doing it this way is powerful, for your initial library doesn't need any change (and often it can't be changed, if it's a 3rd party import) and most importantly, none of the dependants that call display() need to be changed, they pick the addition transparently.
is so you don't have to manually allocate the memory
If you have a proprer OOP language, that has garbage collection, you don't have this problem in any of the techniques we're talking about.
But maybe you're talking about efficiency, and here the thing is if you come from the land of Clean Code, you're used to care about software engineering principles and best practices much more than super-optimised code. When you program a device microcontroller, saving a few bytes is still important, but if you process GBs of genetic data in a cloud, your optimisations are much more about O(n) and operstive research, how many nodes you spin per day and how much man-days you pay on average to debug millions of lines spread in a whole organisation.
The longer you wait to rigidly decide which data is flowing through which code, the more code will be prone to invisible errors and difficult to predict behaviors.
That's true, but when properly used, polymorphism doesn't cause many readability issues, on the contrary, it allows for better separation of concerns and avoids that multiple types are managed in the same giant blob (like a giant if/elseif, which isn't extendable either).
Besides, any language or programming technique can be used very appropriately or very badly.
Something has to be hard typed somewhere, and personally I think "gigantic if statement" or "entire library with function decorators" are the worst options.
Sorry, to me, it sounds like "something has to be translated to Assembly". Except, if the language compiler or interpreter does it for you, it can become much better.
Doing the dispatch manually, though I use it when coding a quick/small prototype, possibly to be refined later, is the worst option from the point of view shown above.
0
4
u/NoRun9890 Jan 04 '23
This is a terrible idea, don't do this in Python. Why are you adding decorators, complexity, and opportunities to create hard to diagnose/understand errors when Python ducktyping already makes "overloading" trivial?
The original example was so much easier to read and maintain:
C++ has function overloading because it CAN'T do this, not because function overloading is better. You're going backwards.