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

Поделиться
HTML-код
  • Опубликовано: 23 дек 2024

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

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

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

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

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

  • @Landon_Hughes
    @Landon_Hughes Год назад +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.

  • @mayo-neighs
    @mayo-neighs Год назад +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

      That's an interesting implementation

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

  • @briceparent593
    @briceparent593 Год назад +7

    Is it better than using partials?

    • @gardnmi
      @gardnmi Год назад +7

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

    • @mwagaha3343
      @mwagaha3343 Год назад +5

      @@gardnmi Yeah, partials are way more readable

  • @biggbluecanoe7108
    @biggbluecanoe7108 Год назад +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?

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

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

    I thought the name for these are closures?

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

    Could this be used for a state machine?

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

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

  • @mwagaha3343
    @mwagaha3343 Год назад +15

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

    • @joaovitorprudente1976
      @joaovitorprudente1976 Год назад +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

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

    That was awesome

  • @KavyanshKhaitan
    @KavyanshKhaitan Год назад +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?

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

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

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

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

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

    it's interesting, thanks 👍

  • @elsheep6951
    @elsheep6951 Год назад +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.

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

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

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

    this is functional programming?

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

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

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

      He provided that explanation at 3:47

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

    This is quite confusing for me.

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

    No

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

    9th

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

    thumbnail is crazy