Тёмный

Functools is one of the MOST USEFUL Python modules 

Carberra
Подписаться 18 тыс.
Просмотров 32 тыс.
50% 1

Functools is definitely one of the more useful packages in the standard lib; it's amazing I haven't covered it yet! Some stuff in functools needs its own video, so look out for further installments in the functools saga!
-
If you enjoy my content, consider supporting me on Patreon or becoming a member!
patreon.carberra.xyz
join.carberra.xyz
If you need help with anything, feel free to join the Discord server:
discord.carberra.xyz
I get a lot of people asking, so here's my Visual Studio Code setup!
• My Visual Studio Code ...
-
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

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

 

6 янв 2023

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 39   
@Carberra
@Carberra Год назад
Slight correction: The @cached_property and the @property @cache decorator combo are functionally identical as I stated. While they do the same thing, they do it in different ways.. For example, you can't use a @cached_property in a slotted class, but you can use the other method just fine. @cached_property also allows writes by default.
@BeesAndSunshine
@BeesAndSunshine Год назад
Careful with an example like the one at 7:00. If instead of 3, x was bound to a mutable object, and then a mutable operation was done to that object, the change would be reflected in partial.
@friendliness1337
@friendliness1337 Год назад
Thanks for the video! Only just discovered your channel and I've already discovered a fair few new things or finally grasp things I didn't fully understand before :) Really like the way you explain things!
@Carberra
@Carberra Год назад
Glad I could help out! (:
@MrJet84
@MrJet84 8 месяцев назад
Very useful lesson. Thank you!
@aaronater1088
@aaronater1088 Год назад
always great stuff man 👌 Definitely will be using cache and partial on some projects
@Carberra
@Carberra Год назад
Thanks! Yeah I've used partial before, but never played too much with caching, something I'll deffo be changing.
@tan.nicolas
@tan.nicolas Год назад
you are amazing man! cheers and thanks from Chile!
@Carberra
@Carberra Год назад
Thank you!
@benjaminfranklin329
@benjaminfranklin329 Год назад
Thanks, I'd not actually seen wraps or cache before. I've been manually implementing these for years...
@JorgeEscobarMX
@JorgeEscobarMX 6 месяцев назад
That partial function seems useful when you don't have all arguments at the same time. Some times you'll need to process something or wait for an api response to get the rest of the arguments.
@miguelvasquez9849
@miguelvasquez9849 7 месяцев назад
great video, i have a question. In the next script, why does the function only print "adding" twice?, i think it should print 3 times, one for each different sequence number. @cache def add(value1, value2): print('adding') return value1 + value2 sequence = [1, 1, 2, 3, 1, 2, 2] for num in sequence: print(add(2, num))
@Carberra
@Carberra 7 месяцев назад
Thanks! Just ran it myself and it prints three times for me. I almost missed the first time it printed adding, maybe you did the same?
@Fine_Mouche
@Fine_Mouche Год назад
10:00 it's weird to me to use time.sleep() and @cache in/on the same function, what can be a true use of that combination ?
@Carberra
@Carberra Год назад
I just used the sleep to represent a function that takes a long time to execute. You'd probably never need to combine the two in the real world.
@benjaminfranklin329
@benjaminfranklin329 Год назад
You might use cache with a function that accesses a slow resource such over a network, where you don't care about any updates that may happen to that resource. If you needed to read a configuration file from say a web server or s3 for example.
@fire17102
@fire17102 Год назад
Really nice channel! How can @cache be exported or saved? I'd like it to not reset every runtime for certain functions and properties :) Thanks🙏 👍
@Carberra
@Carberra Год назад
Thank you, glad you like it! Afaik you can't do that with @cache, but there are some options available: 1. Build your own implementation on top of @cache 2. Use Redis -- it's designed for on-disk caching (so it'll persist between runtimes), and it's ludicrously quick. I don't have a video on the channel, but sounds like it could be a nice idea.
@fire17102
@fire17102 Год назад
@@Carberra thanks for the quick reply! Already using redis :P Can probably make a @cache_redis pretty easily, thanks :)
@Carberra
@Carberra Год назад
Yeah, I think the way my work does it is by creating a decorator and decorating the wrapper function with the @cache deco.
@benjaminfranklin329
@benjaminfranklin329 Год назад
For basic problems where running redis, you could wrap wrap read/write a pickled object. Obviously, this has some security and performance implications, so may not be appropriate
@SonnyGeorgeVlogs
@SonnyGeorgeVlogs Год назад
Joblib's cache does it this way (w/ pickle) (no need to build it yourself)
@johnstarfire
@johnstarfire Год назад
What if you called the wrapper add?
@dragweb7725
@dragweb7725 5 месяцев назад
that only works if you only use the wrapper on the add function, but the goal of a decorator is to be used on many different functions on which you want to add the same bit of actions. In this example if you wrote another function called "substract" you can apply the with_greetings decorator on both add and substract and it will still work properly for both
@Andrumen01
@Andrumen01 7 месяцев назад
I am guessing that if you have some random process in a function, using the @cache decorator will not be convenient. That would be useful for deterministic functions....a word of caution! Also, maybe calculating some Fibonacci with the @cached property would be an interesting exercise!
@Carberra
@Carberra 7 месяцев назад
You've more or less predicted a video going live in a few weeks! I focus specifically on caching there, including a Fibonacci example! 😄
@MechMK1
@MechMK1 Год назад
Please increase the font size in your editor for the video. It's very hard for me to read on my phone.
@johnstarfire
@johnstarfire Год назад
You cAn zoom now
@gedtoon6451
@gedtoon6451 7 месяцев назад
How do you zoom? @@johnstarfire
@ExDarkx3
@ExDarkx3 Год назад
ohhh partial is supposed to be the way to do it i have always been setting up an anonymous lambda function to preload the variables
@NoidoDev
@NoidoDev 8 месяцев назад
partial does here what curry does in other languages.
@sethdhanson
@sethdhanson 6 месяцев назад
If only it were possible to include even MORE unused space on the right and bottom of the screen so that the text was even smaller . . .
@7DYNAMIN
@7DYNAMIN 5 месяцев назад
get a bigger screen
@sethdhanson
@sethdhanson 5 месяцев назад
that's helpful - thanks@@7DYNAMIN
@_Akhilleus_
@_Akhilleus_ Год назад
Ok, half of the screen is useless and the other half is unreadable
@enantiodromia
@enantiodromia Год назад
I think the explanation should be structured more, and terminology like "some weirdness" doesn't help me comprehend the topic either.
@dnaviap
@dnaviap Год назад
Nice way of implementing en.wikipedia.org/wiki/Currying and en.wikipedia.org/wiki/Memoization in Python!
Далее
Python's magic methods
19:30
Просмотров 12 тыс.
Python's contextlib is a HIDDEN GEM
14:55
Просмотров 10 тыс.
5 Useful Python Decorators (ft. Carberra)
14:34
Просмотров 92 тыс.
Pydantic is OP, here's why
18:10
Просмотров 20 тыс.
Modern Python logging
21:32
Просмотров 162 тыс.
Python Standard Library: Functools
33:42
Просмотров 1,6 тыс.
5 Good Python Habits
17:35
Просмотров 432 тыс.
Python Decorators in 15 Minutes
15:14
Просмотров 431 тыс.
Python 101: Learn the 5 Must-Know Concepts
20:00
Просмотров 1,1 млн
Debugging 101: Replace print() with icecream ic()
12:36
Partial functions in Python are SUPER NEAT
8:37
Просмотров 4,3 тыс.
10 Python Comprehensions You SHOULD Be Using
21:35
Просмотров 129 тыс.