How To Write CLEAN & Reusable Code With "Currying" In Python

Поделиться
HTML-код
  • Опубликовано: 6 фев 2025
  • Here's a quick guide that teaches the principles of functional programming and how to apply them to writing clean and efficient code in Python using the technique of "currying." The focus is on writing readable, maintainable, and reusable code.
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

Комментарии •

  • @tomdonaldson8140
    @tomdonaldson8140 Год назад +1

    Nice. Would love to see a comparison of: currying, continuations, closures.

  • @preritdas6998
    @preritdas6998 Год назад +2

    can either use functools.partial or define a single function taking all params then use lambda.
    def multiply(a, b)
    double = partial(multiply, a=2)
    double = lambda b: multiply(2, b)

  • @dexterrity
    @dexterrity Год назад

    I left this comment under another post but figure I'd put it here on its own since I get the feeling some are struggling to see the use case.
    This came up organically for me in work yesterday. I didn't know it was called currying, but I found myself in a situation in R where the most elegant solution to creating custom axis ticks for a visualisation was by creating a function that returned another function.
    example here, apologies for it being in R but I cbfed translating my example into python:
    label_at

  • @SamRPyke
    @SamRPyke Год назад +3

    Another way to do it is with a callable object, something like
    class Multiplier(namedtuple('Multiplier',['amount'])):
    def __call__(self, number):
    return self.amount * number
    It's more verbose than a lambda, but it has a better repr (which is handy when debugging) and you can add other behaviour if you want to.

    • @SamRPyke
      @SamRPyke Год назад +1

      Oh, yeah, you also need to import namedtuple from collections. Whatever; I like having good reprs when I'm debugging.

  • @sharabugnanesh3098
    @sharabugnanesh3098 2 года назад +2

    That was awesome

  • @mayo-neighs
    @mayo-neighs 2 года назад +1

    I made a currying function that returns itself. its benefit is using the benefits of a function-type value without worrying about accidentally calling it because it is the same regardless of how many pairs of parenthesises are after it!

    • @Indently
      @Indently  2 года назад +2

      That's an interesting implementation

  • @biggbluecanoe7108
    @biggbluecanoe7108 2 года назад +3

    Is there a benefit of using this instead of a class and class method, or is this more of a shortcut to achieve a similar result?

  • @briceparent593
    @briceparent593 2 года назад +7

    Is it better than using partials?

    • @gardnmi
      @gardnmi 2 года назад +7

      I would prefer partials in my code base since its more understood and explicit in what you are doing.

    • @mwagaha3343
      @mwagaha3343 2 года назад +5

      @@gardnmi Yeah, partials are way more readable

  • @kajsagauza
    @kajsagauza 8 месяцев назад

    I use functools partial, but I am not sure if those two are equivalent

  • @Landon_Hughes
    @Landon_Hughes 2 года назад +7

    For those unaware, these are also known as higher order functions.
    I.e. functions that take in functions as arguments OR a function that returns a function.

  • @confidencemichaelokojaja7669
    @confidencemichaelokojaja7669 Год назад +2

    I thought the name for these are closures?

  • @sugo8920
    @sugo8920 Год назад

    currying is especially good in app code. you can make library code with a lot of params but in the app code you can abstract it and make more purpose built functions.
    One case I had is a had a library fn that uploads to s3. the s3 fn takes group and key, and upload. The group in my apps was a function of the app name so I used this pattern to create functions that upload and download to certain group/keys to avoid having to pass them around. Only dynamic input was the upload parameter for upload.

  • @mwagaha3343
    @mwagaha3343 2 года назад +15

    Okay this is pretty cool but I don't see how this could ever come up

    • @joaovitorprudente1976
      @joaovitorprudente1976 2 года назад +9

      It's more useful with Partial from functools, which is a function that takes another function and a subset of its parameters and returns a function that takes only the missing parameters and has the already given parameters as constants. I use it a lot in gui programming while styling widgets so that I get consistent widgets and don't have to write the same arguments for each one

    • @dexterrity
      @dexterrity Год назад +2

      This came up organically for me in work yesterday. I didn't know it was called currying, but I found myself in a situation in R where the most elegant solution to creating custom axis ticks for a visualisation was by creating a function that returned another function.
      example here, apologies for it being in R but I cbfed translating my example into python:
      label_at

    • @vikingthedude
      @vikingthedude Год назад

      U wouldn’t really write the currying logic from scratch. You’d use a library. You’d typically use it to compose your dependencies when you do dependency injection without classes

  • @master_sergik
    @master_sergik 2 года назад +1

    it's interesting, thanks 👍

  • @gazdinf
    @gazdinf 2 года назад +2

    Could this be used for a state machine?

  • @KavyanshKhaitan
    @KavyanshKhaitan 2 года назад +2

    I was wondering of this in my school bus today. What a co-incidence!
    edit: i didnt know this was possible in python.
    and can u return a function without making one? something like a lambda function to pass into a function call, but a full function like you can do in JS?

  • @elsheep6951
    @elsheep6951 2 года назад +1

    I have to admit that my first run through this resulted in Eh?! Then the penny dropped. Thanks. I will be rewriting my function for randomly generating numbers of variable sizes a variable number of times. (rolling different types of dice and numbers of them) to start with.

  • @abdultheseekerofknowledge4453
    @abdultheseekerofknowledge4453 2 года назад +2

    Bro could you please make a video about async, multithreading, multiprocessing and GIL

    • @Indently
      @Indently  2 года назад +1

      That's all included in my official course, it will be a long time before I bring it to this channel.

  • @joaoguilhermezati6327
    @joaoguilhermezati6327 11 месяцев назад

    this is functional programming?

  • @dragweb7725
    @dragweb7725 Год назад

    as you're coding in python, i need to say that what you're doing seems to be equivalent to defining decorators

  • @birdie123
    @birdie123 2 года назад +3

    Another way of explanation might be:
    double(10) ==> multiply_setup(2)(10)
    double(100) ==> multiply_setup(2)(100)
    😆

    • @JungleLibrary
      @JungleLibrary 2 года назад +2

      He provided that explanation at 3:47

  • @flyinggoatman
    @flyinggoatman Год назад +1

    This is quite confusing for me.

  • @betapacket
    @betapacket 2 года назад +1

    9th

  • @EverythingTechTime
    @EverythingTechTime 5 месяцев назад

    No

  • @pinklemonade8444
    @pinklemonade8444 2 года назад +1

    thumbnail is crazy