Function Overloading For Python Developers (Singledispatch)
Vložit
- čas přidán 18. 03. 2023
- In this tutorial I will be showing you how you can perform function overloading in Python using singledispatch from functools.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
This is exactly what I needed thank you.
very helpful!
Very interesting. Can't think of when I'd ever use it, but good to know
@@tophat593
Let’s say you have a function called ‘func’ that does something to an integer input. But in the future, you want to make the function also work for other types of input, and for each of those cases, the function should do something different. Doing this allows you to modularize the handling of each of those types of inputs, which might be better than treating all those cases inside the original function in massive if/elif/else blocks.
I think java supports overloading, so it mimics that
I really miss using it from Swift.
@@vinicus508 I'm sure I'm oversimplifying it - but if you are wanting different inputs and outputs shouldn't that be a separate function? If you are always returning the same output but need different/new inputs an optional arg should handle that? (I'm a noob so I could definitely be missing something)
In curious if this also works with dunder methods.. recently I've wanted to have two separate class initiallizers, one that takes individual parameters and one that takes dictionary, as someone who's done fair bit of coding in c++ I got quite disappointed when I found out that python doesn't natively support overloading, which is really useful thing most of the time..
How does this compare to the isinstance checking approach speed-wise and memory-wise?
Can the type be used simultaneously in the decorator and the function definition?
Correction: The pipe syntax for unions is available as of python 3.10, see PEP-604
Hey Federico!
I'm a big fan of your tutorials.
Could you create a video covering 'multiple dispatch'?
I like singledispatch. It's short and pythonic.
👍💯🙏
I don't see the name of overloaded functions as _, what if I have more than 1 function overloaded?
Thumbnail 😢 there is just type annotation 😂 which will be ignored by python interpreter.... And mentioned it as fn overloading 😂😂😂😊 just fun, comments on thumbnail only not content😊
I have an increasingly stronger feeling that people are trying to supercharge Python - a simple scripting language - with concepts which are not natively supported by the language by decorators, behind-the-scenes meta-magic or creating countless tools around it.
Examples - everloading using decorators, pytest with its hidden/implicit fixture and monkeypatch functionality or countless tools to run rigorous type checks/validations (pydantic, mypy, pyre, etc.) in a type-loose languge. It feels kinda like dressing a dog in brand clothes and pretending it is a teenager.
>with concepts which are not natively supported by the language by decorators
how is it not natively supported to just take a function as an argument and do something with it before it's called?
It's not magic, it's just abstraction which is an essential concept in any realm of programming
I think the code could be just as readable if you gave each function its own name