Every Decorator in Python Needs THIS: @wraps

Поділитися
Вставка
  • Опубліковано 15 січ 2025

КОМЕНТАРІ • 40

  • @alessi4249
    @alessi4249 19 днів тому +14

    Very useful for logging with an exception catching wrapper. Thank you great video!

  • @JakobWierzbowski
    @JakobWierzbowski 19 днів тому +2

    Thank you! Your hints are a real treasure!

  • @klmcwhirter
    @klmcwhirter 19 днів тому +14

    A better demo of why wrap is important would be to modify expensive_function to raise an error and see what you get. For example, raise ValueError('check stack trace')

    • @zeroyagami537
      @zeroyagami537 18 днів тому +1

      I just tried this, but there was no difference in Traceback🤔(Python 3.12)

  • @anon_y_mousse
    @anon_y_mousse 16 днів тому +1

    I didn't know this was a problem for Python, so thanks for the warning. I am curious though both how @wraps is implemented as well as whether standard library decorators use it. Maybe do a video on that?

  • @bohdandanylko2006
    @bohdandanylko2006 19 днів тому +1

    You create cool short videos about Python. You also create very good courses, thanks for your job.

  • @UtkarshRana9999
    @UtkarshRana9999 19 днів тому +1

    Learned something new today! 👍🏼 Thanks

  • @DmitryK-u5c
    @DmitryK-u5c 16 днів тому

    Happy New Year mr Indently!

  • @mozambiquesquad7463
    @mozambiquesquad7463 11 днів тому

    That's very useful to make things easier

  • @DanielCordey
    @DanielCordey 8 днів тому

    Thanks for this tip !

  • @Little-bird-told-me
    @Little-bird-told-me 19 днів тому +1

    Great stuff nicely explained

  • @Nikolas_Davis
    @Nikolas_Davis 19 днів тому +6

    It's also very useful so that you still get autocomplete functionality for your wrapped functions; otherwise, information on the names and types of arguments is lost.

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 19 днів тому +1

    Thank you 😊

  • @footflaps
    @footflaps 19 днів тому +1

    Very useful!

  • @oida10000
    @oida10000 18 днів тому +3

    I did not know this and apperently some devolpers also don't know this - I just tested with the function decorator from latexify liberary and it overwrites the doc string. Hey could you write a wrapper function that properily wraps 3rd party decorators below it?

  • @ralvarezb78
    @ralvarezb78 18 днів тому

    Very usefull

  • @derto8i
    @derto8i 17 днів тому +1

    why are you prefering ' instead of "?

    • @Indently
      @Indently  17 днів тому +1

      Less screen clutter, and the odds of me using double quotes inside strings are usually much higher than single quotes.

  • @rick-lafleur
    @rick-lafleur 18 днів тому +1

    The wrapt module has much better decorators for these things

  • @gigores
    @gigores 19 днів тому +3

    what is this green line? is it a new update? is it a plugin?

    • @kerty-
      @kerty- 19 днів тому +2

      It PyCharm Git plugin visualizing file change

  • @albinantony4998
    @albinantony4998 19 днів тому +1

  • @bartuustaoglu4167
    @bartuustaoglu4167 18 днів тому

    I couldn't understand why we opened a function called wrapper in the function. Why doesn't it work if we write it directly into Get Time without opening a function called wrapper (I translated it using Google Translate, it may have been translated poorly)

    • @kiraleskirales
      @kiraleskirales 17 днів тому +1

      The decorator pattern is very useful. Firstly, it can be reused, and secondly it conceptually separates the computation and the time performance.

    • @bartuustaoglu4167
      @bartuustaoglu4167 16 днів тому +1

      ​@kiraleskirales I know the decorative thing too. And what I'm asking is why we write it inside the
      Function():
      wrapper()
      ....
      Return wrapper
      Instead of writing it directly inside the wrapper, why not write it inside the Function? Why is there a need to create a wrapper?

    • @x-_-_-Drayz
      @x-_-_-Drayz 7 днів тому

      @bartuustaoglu4167 Because a decorator is a function. When you do @decorator above a function "f" it's like doing decorator(f). Which is why attribute like name are change without @wrap. To be more specific when you run a script everey @decorator are execute, you can easely see that by printing a value before the wrapper inside the decorator. Another important point is that you need a third function that will output a decorator when call in order to allow parameter inside you decorator like "@get_time(round=2)" instead of "@get_time".

  • @sumitbiswas164
    @sumitbiswas164 19 днів тому +1

    Decorator for decorator
    -> metadecorator 🤔

    • @jmyyer
      @jmyyer 19 днів тому +2

      Chained decorators ?

  • @malted.coffee
    @malted.coffee 19 днів тому +2

    What are the "new*" lines above each function? Looks like they were autogenerated and not part of the script itself; a PyCharm thing perhaps?

    • @kerty-
      @kerty- 19 днів тому +3

      It from PyCharm Git plugin

    • @malted.coffee
      @malted.coffee 18 днів тому +2

      @@kerty- Ah, thanks

    • @MikeSwift.v1
      @MikeSwift.v1 18 днів тому

      @@kerty- I was wondering about that.

  • @binaryblade2
    @binaryblade2 17 днів тому

    Lol,no async support

  • @mithrial1
    @mithrial1 18 днів тому +20

    Please disable the automatic dubbing. I can see the benefit for many users but I'm fluent in English and I don't want it in my local language.

    • @Indently
      @Indently  18 днів тому +5

      I don't have that option in the settings. Did you check your UA-cam settings?

    • @Indently
      @Indently  18 днів тому +13

      From a quick Google search, this is fully automatic and neither of us have the option to manually disable it.
      It might work if you change all of your UA-cam to English,

    • @miy30vh
      @miy30vh 18 днів тому

      Nein, bitte lass alles so wie es ist. Ich möchte gerne das jeder gleichermaßen den Schwachsinn von Google und UA-cam ertragen muß und niemand sich freikaufen kann. Gleiches Recht für alle! Muß gelten.

    • @eaglegosuperskarmor
      @eaglegosuperskarmor 14 днів тому

      In the video settings you should be able to select the audio track. I haven't checked my computer but it works on my phone at least

    • @mithrial1
      @mithrial1 14 днів тому

      @@eaglegosuperskarmor sure, I can do this manually. But it doesn't stick, the next video will be auto-dubbed again.