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
Nice. Would love to see a comparison of: currying, continuations, closures.
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)
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
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.
Oh, yeah, you also need to import namedtuple from collections. Whatever; I like having good reprs when I'm debugging.
That was awesome
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!
That's an interesting implementation
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?
Is it better than using partials?
I would prefer partials in my code base since its more understood and explicit in what you are doing.
@@gardnmi Yeah, partials are way more readable
I use functools partial, but I am not sure if those two are equivalent
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.
I thought the name for these are closures?
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.
Okay this is pretty cool but I don't see how this could ever come up
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
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
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
it's interesting, thanks 👍
Could this be used for a state machine?
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?
wow that was a quick response
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.
Bro could you please make a video about async, multithreading, multiprocessing and GIL
That's all included in my official course, it will be a long time before I bring it to this channel.
this is functional programming?
as you're coding in python, i need to say that what you're doing seems to be equivalent to defining decorators
Another way of explanation might be:
double(10) ==> multiply_setup(2)(10)
double(100) ==> multiply_setup(2)(100)
😆
He provided that explanation at 3:47
This is quite confusing for me.
9th
No
thumbnail is crazy