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

  Рет қаралды 33,234

Indently

Indently

Күн бұрын

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:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
00:00 Intro
00:31 What is currying?
02:36 What are partials?
06:07 What’s the difference?
07:39 Summing it up

Пікірлер: 50
@SuperVirus1978
@SuperVirus1978 Жыл бұрын
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.
@domenicfieldhouse5644
@domenicfieldhouse5644 Жыл бұрын
You would put f"a={a}, b={b}"
@chrishutchinson3827
@chrishutchinson3827 Жыл бұрын
What Super Virus said will work.
@theolephay
@theolephay Жыл бұрын
@@domenicfieldhouse5644 What @supervirus wrote is correct and would do the exact same thing, but it’s simpler and easier to use (in case of renaming for example)
@willladerer1293
@willladerer1293 Жыл бұрын
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
@hakushara
@hakushara Жыл бұрын
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!
@Boris_Belomor
@Boris_Belomor 11 ай бұрын
I don't know... I would personally prefer to reuse dictionary of parameters in that case. Less chance of an error.
@armynyus9123
@armynyus9123 4 ай бұрын
Raymond for the win. I support EVERYTHING he does, total fan. Even the rejected stuff I support, all the power to the dev.
@vorpal22
@vorpal22 Жыл бұрын
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.
@chriskeo392
@chriskeo392 4 ай бұрын
I thought I'd never come across it...but then I did 😂 And I came right back to this video
@jamesbond_007
@jamesbond_007 2 ай бұрын
Very informative! The partial method is very interesting in all its flexibility.
@ilyalalala7655
@ilyalalala7655 Жыл бұрын
I think that question is under every video but… what is this great theme? 😍
@mehtubbhai9709
@mehtubbhai9709 Жыл бұрын
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
@resresres1
@resresres1 6 ай бұрын
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.
@walker-gx1lx
@walker-gx1lx Жыл бұрын
"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.
@senayan6823
@senayan6823 Жыл бұрын
It works with class constructor ? Could be very usefull too !
@laytonjr6601
@laytonjr6601 2 ай бұрын
I usually use lambda to create partial functions, I forgot that everything can be done more easily by importing stuff
@WaldoTheWombat
@WaldoTheWombat 14 күн бұрын
Thanks friendo! Are you from Germany?
@stuartfranz5484
@stuartfranz5484 Жыл бұрын
Can you do this for functions defined in a class?
@Indently
@Indently Жыл бұрын
Yup, they have something called partialmethod for that
@Alister222222
@Alister222222 Жыл бұрын
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.
@naomicoffman1315
@naomicoffman1315 4 ай бұрын
Much like any technique, you shouldn't adopt currying as a default; you should use it when it actually helps. This isn't a perfect example, but a bit of Lua I wrote for an indie game might provide a useful example: ``` local function parseTabledItem(source, label) return function() local name = parse.getString() local result = source[name] if not (result and result:isValid()) then parse.displayMessage('Unknown ' .. label .. '"' .. name .. '"', true) end return result end end local parseShip = parseTabledItem(tb.ShipClasses, 'ship class') local parseWeapon = parseTabledItem(tb.WeaponClasses, 'weapon class') local parseIntel = parseTabledItem(tb.IntelEntries, 'intel entry') ``` `parseTabledItem` here behaves as a function factory. I use it to give names to the concepts of parsing the name of a ship, weapon, or intel item, which are then used throughout the rest of the script. There are other ways to do it, but they would have been longer and, imo, not communicate intent as clearly. (As a caveat to my point at the start, there are languages like Haskell where *all* functions are curried... however, in those languages, it's built into the syntax at a very low level, and you don't usually have to think about it.)
@barelycodingtoday
@barelycodingtoday Жыл бұрын
So this is similar to Recursion than? This is really helpful, thanks for showing me this
@Nerdimo
@Nerdimo 4 ай бұрын
No, recursion is a function that calls itself within itself until some base case is reached (otherwise you’ll encounter a stack overflow). Think of this as literally returning a function as a variable. The variable is a function just as if it was defined using the def keyword. This is because Python has “first class” functions, meaning functions can be variables too.
@abdiellara7816
@abdiellara7816 Жыл бұрын
Great
@redserpent
@redserpent Жыл бұрын
def multiply_setup(a: float): def multiply(b: float) -->: how do you get that dash-arrow to work. ? Never seen it before. And my IDE won't process it. Am using [ --> ]
@muralidhar2152
@muralidhar2152 Жыл бұрын
Use -> Ex: def multiple_setup(a:float) ->: print(a)
@jordanrozum
@jordanrozum 11 ай бұрын
You need to use a font & IDE that supports ligatures. I use VS Code with the FiraCode font (you have to download & install the font first), but there are lots of other options too.
@neneodonkor
@neneodonkor 6 күн бұрын
What ​@@jordanrozum said. However, in this case JetBrains Mono was used.
@shariqueansari9439
@shariqueansari9439 Жыл бұрын
Can you pls tell me difference between curing and closure? It seems curing looks like closure 🤔
@naomicoffman1315
@naomicoffman1315 4 ай бұрын
Currying is the process of transforming a function (a, b) -> c into a function a -> b -> c, and so on for higher arities. It's often implemented as closure, and is in Python.
@stevenlynch3456
@stevenlynch3456 4 ай бұрын
How is this different than Closures (for example in JavaScript)?
@naomicoffman1315
@naomicoffman1315 4 ай бұрын
Currying is the process of transforming a function (a, b) -> c into a function a -> b -> c, and so on for higher arities. It's often implemented as closure, and is in Python.
@Randych
@Randych 4 ай бұрын
"First they laugh at you, and then they tryhard to at least pretend they are you" - Haskell
@nickxox2257
@nickxox2257 11 ай бұрын
what code editor is that?
@user-cg7yd4lx3i
@user-cg7yd4lx3i 4 ай бұрын
Looks like it’s jetbrains pycharm
@Mustlight
@Mustlight Жыл бұрын
Whick ide is that?
@himmelsdemon
@himmelsdemon 4 ай бұрын
Pycharm or IntelliJ Idea
@RedHair651
@RedHair651 4 ай бұрын
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) ```
@mahmodi5timetolearn
@mahmodi5timetolearn Жыл бұрын
And it's the same as kwargs & args
@Sinke_100
@Sinke_100 Жыл бұрын
Why does curing can't take multiple parameters, you can surely defined it that way
@AWriterWandering
@AWriterWandering Жыл бұрын
I think he meant multiple different parameters. You would have to define the inner and outer functions for each case. I mean you could use named Params, and then have it be a function factory. But that gets complicated. Partial makes it a lot easier.
@Sinke_100
@Sinke_100 Жыл бұрын
@@AWriterWandering yes, i agree, I rarely find a case to use either though
@kingoreo7050
@kingoreo7050 Жыл бұрын
It’s based off of lambda calculus, where any function only has one input and one output, so you have to curry to have multi variable functions. In Python it seems a lot less useful
@godowskygodowsky1155
@godowskygodowsky1155 Ай бұрын
​@@AWriterWanderingI don't get what you mean. def outer(a, b, c): def inner(x, y, z): return f(a, b, c, x, y, z) return inner Currying is A x X -> Y is isomorphic to A -> (X -> Y). You can replace A and X with product types, and it will still be valid.
@Mrbotosty
@Mrbotosty Жыл бұрын
What is this IDE?
@neneodonkor
@neneodonkor 6 күн бұрын
JetBrains PyCharm
@Th10Fan
@Th10Fan Жыл бұрын
Python programmers: we can use currying and partials! Here is 5-10 lines of code to do that! Haskell programmers: multiply x y = x * y double = multiply 2 triple = multiply 3 double 5 > 10 triple 9 > 27
@RedHair651
@RedHair651 4 ай бұрын
What you did is also possible in Python with lambda functions.
@y2ksw1
@y2ksw1 3 ай бұрын
Headache functionality.
Exec() VS Eval() Explained In Python Tutorial 2023
6:44
Indently
Рет қаралды 18 М.
10 Ways You Can Use "_" In Python (Do you know ALL of them?)
9:56
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 32 МЛН
Red❤️+Green💚=
00:38
ISSEI / いっせい
Рет қаралды 82 МЛН
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 57 М.
You Can Do Really Cool Things With Functions In Python
19:47
ArjanCodes
Рет қаралды 219 М.
3 Bad Python Habits To Avoid
10:40
Indently
Рет қаралды 49 М.
How To Use List Comprehension In Python
6:41
Taylor's Software
Рет қаралды 6 М.
Abstraction Can Make Your Code Worse
5:13
CodeAesthetic
Рет қаралды 630 М.
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 55 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 101 М.
5 Tips To Write Better Python Functions
15:59
Indently
Рет қаралды 98 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 55 М.
Debugging 101: Replace print() with icecream ic()
12:36
NeuralNine
Рет қаралды 361 М.
Now THIS is entertainment! 🤣
00:59
America's Got Talent
Рет қаралды 40 МЛН