Function Overloading For Python Developers (Singledispatch)

Sdílet
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

Komentáře • 19

  • @ranjit9427
    @ranjit9427 Před rokem +7

    This is exactly what I needed thank you.

  • @sojino
    @sojino Před rokem +1

    very helpful!

  • @murphygreen8484
    @murphygreen8484 Před rokem +4

    Very interesting. Can't think of when I'd ever use it, but good to know

    • @vinicus508
      @vinicus508 Před rokem

      @@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.

    • @tinahalder8416
      @tinahalder8416 Před rokem

      I think java supports overloading, so it mimics that

    • @Indently
      @Indently  Před rokem +1

      I really miss using it from Swift.

    • @murphygreen8484
      @murphygreen8484 Před rokem

      @@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)

  • @MyManJohnny
    @MyManJohnny Před rokem +2

    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..

  • @VojtechMach
    @VojtechMach Před rokem

    How does this compare to the isinstance checking approach speed-wise and memory-wise?

  • @pschweitzer524
    @pschweitzer524 Před rokem

    Can the type be used simultaneously in the decorator and the function definition?

  • @xoptics
    @xoptics Před 7 měsíci

    Correction: The pipe syntax for unions is available as of python 3.10, see PEP-604

  • @MrBirjand
    @MrBirjand Před 2 měsíci

    Hey Federico!
    I'm a big fan of your tutorials.
    Could you create a video covering 'multiple dispatch'?

  • @markmilan57
    @markmilan57 Před 8 měsíci

    I like singledispatch. It's short and pythonic.

  • @rishiraj2548
    @rishiraj2548 Před rokem +1

    👍💯🙏

  • @Ak4n0
    @Ak4n0 Před 3 měsíci

    I don't see the name of overloaded functions as _, what if I have more than 1 function overloaded?

  • @tech-learner4555
    @tech-learner4555 Před 10 měsíci +2

    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😊

  • @VojtechMach
    @VojtechMach Před rokem +2

    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.

    • @alexisskodak
      @alexisskodak Před rokem +1

      >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

  • @davidznidarsic5497
    @davidznidarsic5497 Před 8 měsíci +1

    I think the code could be just as readable if you gave each function its own name