Use THIS To Make Your Code MUCH MORE Reusable In Python (Partials)
2024 ж. 17 Мам.
30 775 Рет қаралды
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
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.
You would put f"a={a}, b={b}"
What Super Virus said will work.
@@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)
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
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!
I don't know... I would personally prefer to reuse dictionary of parameters in that case. Less chance of an error.
Raymond for the win. I support EVERYTHING he does, total fan. Even the rejected stuff I support, all the power to the dev.
Very informative! The partial method is very interesting in all its flexibility.
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.
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.
I thought I'd never come across it...but then I did 😂 And I came right back to this video
"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.
I think that question is under every video but… what is this great theme? 😍
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
It works with class constructor ? Could be very usefull too !
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 [ --> ]
Use -> Ex: def multiple_setup(a:float) ->: print(a)
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.
Can you do this for functions defined in a class?
Yup, they have something called partialmethod for that
what code editor is that?
Looks like it’s jetbrains pycharm
"First they laugh at you, and then they tryhard to at least pretend they are you" - Haskell
Great
How is this different than Closures (for example in JavaScript)?
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.
Can you pls tell me difference between curing and closure? It seems curing looks like closure 🤔
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.
So this is similar to Recursion than? This is really helpful, thanks for showing me this
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.
What is this IDE?
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.
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.)
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) ```
Whick ide is that?
Pycharm or IntelliJ Idea
And it's the same as kwargs & args
Why does curing can't take multiple parameters, you can surely defined it that way
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.
@@AWriterWandering yes, i agree, I rarely find a case to use either though
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
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
What you did is also possible in Python with lambda functions.
Headache functionality.