Use THIS To Make Your Code MUCH MORE Reusable In Python (Partials)

preview_player
Показать описание
In this video we are going to be looking at a way to simplify your functions in Python, this will result in more reusable and clean code which is usually the goal a lot of us have when creating bigger projects.

We are going to be using partials from functools, which can be seen as similar to currying, but much more customisable.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Intro
00:31 What is currying?
02:36 What are partials?
06:07 What’s the difference?
07:39 Summing it up
Рекомендации по теме
Комментарии
Автор

Hi, you use f-strings for printing variables with their name you can do f"{a=} {b=}".
If a were 2 and b 7 this would print a=2 b=7.

SuperVirus
Автор

The end of the video definitely did a good job of explaining the use case. I would try to incorporate that earlier next time though. This is cool content, thanks

willladerer
Автор

Wow, this will definitely clean up my code. There are often times that I call a function with a large set of params and this will make it more readable, thanks!

hakushara
Автор

Raymond for the win. I support EVERYTHING he does, total fan. Even the rejected stuff I support, all the power to the dev.

armynyus
Автор

Very informative! The partial method is very interesting in all its flexibility.

jamesbond_
Автор

Well, I definitely don't use currying in Python, because it feels sloppy and there are partials, so I've come across no real use case for them in my coding, although I do certainly have code that returns functions that depend on the parameters. I suppose you could write that via currying, but it isn't necessary.

When I'm doing functional programming in Kotlin or Haskell or Scala, though, currying is obviously incredibly useful. In Haskell, functions are by default defined by currying.

vorpal
Автор

sure this creates "cleaner" code, because you are breaking down a function call into 2 lines of code that are shorter, rather than 1 longer line of code.... but I argue it actually makes it more difficult to understand and read the code.

resresres
Автор

"partial" indeed make code beautiful. I tried it after watching your video, but it's a pity that pycharm doesn't support prompts for functions created via "partial" well. In the example here, after typing "double" when we want to call this function, hovering over it can not show prompts of input parameter names, so if it's imported to be used in another module or file, we need to go back to the source code of it to see what should be passed as input if we do not remember what's needed clearly.

walker-gxlx
Автор

I think that question is under every video but… what is this great theme? 😍

ilyalalala
Автор

You can definitely curry with more than one argument, but maybe that isn't what you actually meant.

erichlf
Автор

I was hoping you would give a more concrete eg. of how partial application could be used for functions that have a lot of params. The eg. you gave of the function that takes a url just breezed thru it. Thanks

mehtubbhai
Автор

I have never used either, and looking at it I feel like you'd be creating labyrinthine code that is just really convoluted. If you built up a base of code that extensively used partials or currying, it seems to me like any change in how the data gets represented or processed could break literally everything, and good luck working backwards through the spaghetti loops of code to figure out how to fix it.

Alister
Автор

wait. when I was looking at someting similar to this, they called it closure. They used it for functional programming where they capture a value and the closure makes the whole thing immutable, so that each input always gave the same output, independently of state. Here, when you created a closure, precluded any side effects from your code. I mean that is what it looks like to me. I didn't know this was called currying. I thought it was a closure.

pokerchannel
Автор

"First they laugh at you, and then they tryhard to at least pretend they are you" - Haskell

Randych
Автор

So this is similar to Recursion than?

This is really helpful, thanks for showing me this

barelycodingtoday
Автор

I usually use lambda to create partial functions, I forgot that everything can be done more easily by importing stuff

laytonjr
Автор

def multiply_setup(a: float):
def multiply(b: float) -->:

how do you get that dash-arrow<dash.dash.greater than> to work. ?
Never seen it before. And my IDE won't process it.
Am using [ --> ]

redserpent
Автор

It works with class constructor ? Could be very usefull too !

senayan
Автор

Can you do this for functions defined in a class?

stuartfranz
Автор

The only reason I can see for using this is to have different function options with the same parameters, otherwise I don't see the point.

```
from functools import partial

operator = "plus"
a = 3
b = 5

def times(a, b):
return a*b

def plus(a, b):
return a+b

result = partial(plus if operator == "plus" else times, a, b)
```

RedHair