Functools is one of the MOST USEFUL Python modules

Sdílet
Vložit
  • čas přidán 6. 01. 2023
  • 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

Komentáře • 39

  • @Carberra
    @Carberra  Před rokem +9

    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 Před rokem +13

    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 Před rokem +2

    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!

  • @aaronater1088
    @aaronater1088 Před rokem +2

    always great stuff man 👌 Definitely will be using cache and partial on some projects

    • @Carberra
      @Carberra  Před rokem +1

      Thanks! Yeah I've used partial before, but never played too much with caching, something I'll deffo be changing.

  • @MrJet84
    @MrJet84 Před 8 měsíci

    Very useful lesson. Thank you!

  • @tan.nicolas
    @tan.nicolas Před rokem

    you are amazing man! cheers and thanks from Chile!

  • @benjaminfranklin329
    @benjaminfranklin329 Před rokem

    Thanks, I'd not actually seen wraps or cache before. I've been manually implementing these for years...

  • @JorgeEscobarMX
    @JorgeEscobarMX Před 6 měsíci

    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.

  • @Fine_Mouche
    @Fine_Mouche Před rokem

    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  Před rokem +5

      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 Před rokem +1

      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.

  • @miguelvasquez9849
    @miguelvasquez9849 Před 8 měsíci

    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  Před 8 měsíci

      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?

  • @fire17102
    @fire17102 Před rokem

    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  Před rokem +1

      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 Před rokem +1

      @@Carberra thanks for the quick reply! Already using redis :P
      Can probably make a @cache_redis pretty easily, thanks :)

    • @Carberra
      @Carberra  Před rokem

      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 Před rokem

      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 Před rokem +1

      Joblib's cache does it this way (w/ pickle) (no need to build it yourself)

  • @johnstarfire
    @johnstarfire Před rokem

    What if you called the wrapper add?

    • @dragweb7725
      @dragweb7725 Před 5 měsíci

      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

  • @MechMK1
    @MechMK1 Před rokem +3

    Please increase the font size in your editor for the video. It's very hard for me to read on my phone.

  • @Andrumen01
    @Andrumen01 Před 8 měsíci

    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  Před 8 měsíci +1

      You've more or less predicted a video going live in a few weeks! I focus specifically on caching there, including a Fibonacci example! 😄

  • @ExDarkx3
    @ExDarkx3 Před rokem +1

    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 Před 8 měsíci

    partial does here what curry does in other languages.

  • @sethdhanson
    @sethdhanson Před 7 měsíci

    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 Před 5 měsíci

      get a bigger screen

    • @sethdhanson
      @sethdhanson Před 5 měsíci

      that's helpful - thanks@@7DYNAMIN

  • @_Akhilleus_
    @_Akhilleus_ Před rokem +1

    Ok, half of the screen is useless and the other half is unreadable

  • @enantiodromia
    @enantiodromia Před rokem +1

    I think the explanation should be structured more, and terminology like "some weirdness" doesn't help me comprehend the topic either.

  • @dnaviap
    @dnaviap Před rokem

    Nice way of implementing en.wikipedia.org/wiki/Currying and en.wikipedia.org/wiki/Memoization in Python!