I use partial functions in tkinter. Say you have a whole load of buttons that you want to use the same call back function. You can make the call back function take an argument specifying which button was pressed. But the button wants a callback function with zero args so you can use a partial function.
@DT-hb3zu6 ай бұрын
Oh that's a great use. I always just use lambda's 😂
@edgarcabrera6026 ай бұрын
Looks like the functools module is implemented in c instead of python, which is interesting. I didn't read the code for partial carefully, but there is something interesting, looks like it doesn't add anything extra to the call stack. I tried something like: def x(a, b): raise Exception("test") y = partial(x, 1) y(2) That shows this traceback: Traceback (most recent call last): File "", line 1, in File "", line 2, in x Exception: test If y is instead a lambda or wrapper function then we have; Traceback (most recent call last): File "", line 1, in File "", line 2, in y File "", line 2, in x Which kind of makes it look like the function is being rewritten.
@Carberra6 ай бұрын
That would certain explain why it's faster to execute if there's one less function call. Would be interesting to see the differences using dis.
@nascentnaga6 ай бұрын
this is very interesting. In engineering we have formulas that often have different keyword argument values for units. It could be nice to use a partial to interchange metric and imperial units for example for constants
@advik-b6 ай бұрын
Underrated channel, also that bri-ish accent is woowwwwww
@Carberra6 ай бұрын
Fanx m8 😄
@cosmiclattemusic6 ай бұрын
yo, ur content is amazing, tysm. BTW please, what is that Terminal configuration that it looks sooo good, is it any Vim related? luv u man
@Carberra6 ай бұрын
Thank you! There's a link in the description to my terminal setup 😄
@cosmiclattemusic6 ай бұрын
@@Carberra tyyyy
@Axman66 ай бұрын
As a functional programmer from Canberra, this bizarro world video title and channel name was very confusing. Glad to see Python providing ways to do programming the way it should be! Can you partially apply more than one argument at a time? Like can you make a partial from a three argument method, then apply it to one argument, then apply that to the second?
@Carberra6 ай бұрын
I wondered if that would happen some day! Genuinely a complete accident the names are so close lmao. To answer your question, yes, you can pass as many arguments as you like, and you can pass kwargs as well! So where you had def func(x, y, z): ... You could do p = partial(func, 1, 2) p(3)
@Axman66 ай бұрын
@@Carberra ha, well glad I could be the first - where’d the name come from? My question is more if you can apply partial to the result of applying partial: x = partial(func,1) y = partial(x,True) z = y(“hello”)
@Carberra6 ай бұрын
Believe it or not it came from the TVR Cerbera -- used to play a lot of Gran Turismo, Carberra just came from that 😅 Just tried it cos I was curious and looks like it works fine yeah.
@alexdeathway6 ай бұрын
5:04 So it happens to best of us.
@Carberra6 ай бұрын
Indeed it does!
@orterves6 ай бұрын
Curried python. Yum. 4:09 I'm not sure why partials are more optimised but my uneducated guess is maybe lambda is a closure and has that overhead, whereas partial creates a function and doesn't have that problem (and partial probably handles the wiring up at a layer closer to the bare metal)