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.

    @SuperVirus1978@SuperVirus1978 Жыл бұрын
    • You would put f"a={a}, b={b}"

      @domenicfieldhouse5644@domenicfieldhouse5644 Жыл бұрын
    • What Super Virus said will work.

      @chrishutchinson3827@chrishutchinson3827 Жыл бұрын
    • @@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)

      @theolephay@theolephay Жыл бұрын
  • 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

    @willladerer1293@willladerer1293 Жыл бұрын
  • 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@hakushara Жыл бұрын
    • I don't know... I would personally prefer to reuse dictionary of parameters in that case. Less chance of an error.

      @BorisBelomor@BorisBelomor9 ай бұрын
  • Raymond for the win. I support EVERYTHING he does, total fan. Even the rejected stuff I support, all the power to the dev.

    @armynyus9123@armynyus91232 ай бұрын
  • Very informative! The partial method is very interesting in all its flexibility.

    @jamesbond_007@jamesbond_00717 күн бұрын
  • 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.

    @resresres1@resresres14 ай бұрын
  • 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.

    @vorpal22@vorpal2211 ай бұрын
    • I thought I'd never come across it...but then I did 😂 And I came right back to this video

      @chriskeo392@chriskeo3922 ай бұрын
  • "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-gx1lx@walker-gx1lx11 ай бұрын
  • I think that question is under every video but… what is this great theme? 😍

    @ilyalalala7655@ilyalalala765511 ай бұрын
  • 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

    @mehtubbhai9709@mehtubbhai970910 ай бұрын
  • It works with class constructor ? Could be very usefull too !

    @senayan6823@senayan682310 ай бұрын
  • 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 [ --> ]

    @redserpent@redserpent11 ай бұрын
    • Use -> Ex: def multiple_setup(a:float) ->: print(a)

      @muralidhar2152@muralidhar215211 ай бұрын
    • 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.

      @jordanrozum@jordanrozum9 ай бұрын
  • Can you do this for functions defined in a class?

    @stuartfranz5484@stuartfranz5484 Жыл бұрын
    • Yup, they have something called partialmethod for that

      @Indently@Indently Жыл бұрын
  • what code editor is that?

    @nickxox2257@nickxox22579 ай бұрын
    • Looks like it’s jetbrains pycharm

      @user-cg7yd4lx3i@user-cg7yd4lx3i2 ай бұрын
  • "First they laugh at you, and then they tryhard to at least pretend they are you" - Haskell

    @Randych@Randych2 ай бұрын
  • Great

    @abdiellara7816@abdiellara7816 Жыл бұрын
  • How is this different than Closures (for example in JavaScript)?

    @stevenlynch3456@stevenlynch34562 ай бұрын
    • 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.

      @naomicoffman1315@naomicoffman1315Ай бұрын
  • Can you pls tell me difference between curing and closure? It seems curing looks like closure 🤔

    @shariqueansari9439@shariqueansari943910 ай бұрын
    • 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.

      @naomicoffman1315@naomicoffman1315Ай бұрын
  • So this is similar to Recursion than? This is really helpful, thanks for showing me this

    @barelycodingtoday@barelycodingtoday Жыл бұрын
    • 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.

      @Nerdimo@Nerdimo2 ай бұрын
  • What is this IDE?

    @Mrbotosty@Mrbotosty10 ай бұрын
  • 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.

    @Alister222222@Alister22222211 ай бұрын
    • 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.)

      @naomicoffman1315@naomicoffman1315Ай бұрын
  • 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) ```

    @RedHair651@RedHair6512 ай бұрын
  • Whick ide is that?

    @Mustlight@Mustlight10 ай бұрын
    • Pycharm or IntelliJ Idea

      @himmelsdemon@himmelsdemon2 ай бұрын
  • And it's the same as kwargs & args

    @mahmodi5timetolearn@mahmodi5timetolearn Жыл бұрын
  • Why does curing can't take multiple parameters, you can surely defined it that way

    @Sinke_100@Sinke_100 Жыл бұрын
    • 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@AWriterWandering Жыл бұрын
    • @@AWriterWandering yes, i agree, I rarely find a case to use either though

      @Sinke_100@Sinke_100 Жыл бұрын
    • 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

      @kingoreo7050@kingoreo705010 ай бұрын
  • 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

    @Th10Fan@Th10Fan10 ай бұрын
    • What you did is also possible in Python with lambda functions.

      @RedHair651@RedHair6512 ай бұрын
  • Headache functionality.

    @y2ksw1@y2ksw129 күн бұрын
KZhead