Тёмный
No video :(

Partial functions in Python are SUPER NEAT 

Carberra
Подписаться 19 тыс.
Просмотров 4,6 тыс.
50% 1

Partials are a little known of Python that can prove incredibly useful! They allow you to create partial objects in place of brand new functions, making code readable, as well as a little more performant!
-
If you want to see more of me, join my Discord server!
/ discord
I get a lot of people asking, so here's my setup!
• Visual Studio Code: • My Visual Studio Code ...
• Terminal: • Make your terminal loo...
-
If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.
#python #coding #howto

Опубликовано:

 

29 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@TonyDig100
@TonyDig100 2 месяца назад
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 2 месяца назад
Oh that's a great use. I always just use lambda's 😂
@edgarcabrera602
@edgarcabrera602 2 месяца назад
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 2 месяца назад
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 2 месяца назад
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 2 месяца назад
Underrated channel, also that bri-ish accent is woowwwwww
@Carberra
@Carberra 2 месяца назад
Fanx m8 😄
@cosmiclattemusic
@cosmiclattemusic 2 месяца назад
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 2 месяца назад
Thank you! There's a link in the description to my terminal setup 😄
@cosmiclattemusic
@cosmiclattemusic 2 месяца назад
@@Carberra tyyyy
@Axman6
@Axman6 2 месяца назад
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 2 месяца назад
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 2 месяца назад
@@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 2 месяца назад
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.
@pythonwithjames
@pythonwithjames 2 месяца назад
Really nice!
@Carberra
@Carberra 2 месяца назад
Thank you!
@murphygreen8484
@murphygreen8484 2 месяца назад
What a coincidence, I'm eating curry right now
@orterves
@orterves 2 месяца назад
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)
@alexdeathway
@alexdeathway 2 месяца назад
5:04 So it happens to best of us.
@Carberra
@Carberra 2 месяца назад
Indeed it does!
@callyral
@callyral 2 месяца назад
Currying?
@KavyanshKhaitan
@KavyanshKhaitan 2 месяца назад
You dropped a pin 📌 Can i have it?
@KavyanshKhaitan
@KavyanshKhaitan 2 месяца назад
FIRST
Далее
SIMPLIFY your code with decorators (+ typing)
13:57
Просмотров 3,8 тыс.
Are descriptors the most MISUNDERSTOOD part of Python?
16:42
New Dyna Skin is OP🥵🔥 | Brawl Stars
00:16
Просмотров 604 тыс.
You Can Do Really Cool Things With Functions In Python
19:47
Pydantic is OP, here's why
18:10
Просмотров 22 тыс.
Deques can be FASTER than lists in Python
12:17
Просмотров 26 тыс.
Dear Functional Bros
16:50
Просмотров 498 тыс.
Please Master These 10 Python Functions…
22:17
Просмотров 142 тыс.
Python's contextlib is a HIDDEN GEM
14:55
Просмотров 11 тыс.
Numba makes your code FASTER with ONE decorator
16:49
Просмотров 4,1 тыс.
5 Useful Python Decorators (ft. Carberra)
14:34
Просмотров 98 тыс.
*Args and **Kwargs in Python
3:49
Просмотров 269 тыс.