Partial functions in Python are SUPER NEAT

  Рет қаралды 5,053

Carberra

Carberra

Күн бұрын

Пікірлер: 29
@TonyDig100
@TonyDig100 6 ай бұрын
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-hb3zu
@DT-hb3zu 6 ай бұрын
Oh that's a great use. I always just use lambda's 😂
@edgarcabrera602
@edgarcabrera602 6 ай бұрын
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.
@Carberra
@Carberra 6 ай бұрын
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.
@nascentnaga
@nascentnaga 6 ай бұрын
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-b
@advik-b 6 ай бұрын
Underrated channel, also that bri-ish accent is woowwwwww
@Carberra
@Carberra 6 ай бұрын
Fanx m8 😄
@cosmiclattemusic
@cosmiclattemusic 6 ай бұрын
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
@Carberra
@Carberra 6 ай бұрын
Thank you! There's a link in the description to my terminal setup 😄
@cosmiclattemusic
@cosmiclattemusic 6 ай бұрын
@@Carberra tyyyy
@Axman6
@Axman6 6 ай бұрын
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?
@Carberra
@Carberra 6 ай бұрын
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)
@Axman6
@Axman6 6 ай бұрын
@@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”)
@Carberra
@Carberra 6 ай бұрын
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.
@alexdeathway
@alexdeathway 6 ай бұрын
5:04 So it happens to best of us.
@Carberra
@Carberra 6 ай бұрын
Indeed it does!
@orterves
@orterves 6 ай бұрын
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)
@pythonwithjames
@pythonwithjames 6 ай бұрын
Really nice!
@Carberra
@Carberra 6 ай бұрын
Thank you!
@murphygreen8484
@murphygreen8484 6 ай бұрын
What a coincidence, I'm eating curry right now
@callyral
@callyral 6 ай бұрын
Currying?
@KavyanshKhaitan
@KavyanshKhaitan 6 ай бұрын
You dropped a pin 📌 Can i have it?
@KavyanshKhaitan
@KavyanshKhaitan 6 ай бұрын
FIRST
SIMPLIFY your code with decorators (+ typing)
13:57
Carberra
Рет қаралды 4,3 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
Advent of Code 2024 - Day 12 - Rust
30:15
Slimsy Beetle
Рет қаралды 45
Pydantic is OP, here's why
18:10
Carberra
Рет қаралды 24 М.
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 68 М.
Deques can be FASTER than lists in Python
12:17
Carberra
Рет қаралды 27 М.
Chess App - Train you Programming Logic #01
40:30
Frontend Road
Рет қаралды 873
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 65 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 109 М.
Python itertools - The key to mastering iteration
20:03
mCoding
Рет қаралды 36 М.
Python's contextlib is a HIDDEN GEM
14:55
Carberra
Рет қаралды 12 М.
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 162 М.
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН