HOW TO USE DECORATORS IN PYTHON 2022 (THE RIGHT WAY)

Поділитися
Вставка
  • Опубліковано 12 жов 2022
  • In this video I will be showing you how you can use decorators in Python which can help make your code cleaner, and much easier to read.
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

КОМЕНТАРІ • 59

  • @Indently
    @Indently  Рік тому +22

    As one of you kindly pointed out, "*kwargs" should actually be "**kwargs" to function properly inside the wrapper. I happened to miss that in the video, sorry about that!

    • @davidmurphy563
      @davidmurphy563 8 місяців тому

      I was the whole video waiting for the code to fail on you but you never sent a kwarg in the end to find the bug. Easily done. :) Particularly nice lesson this one. I never add doc stings so don't use wrap but if you write a library it absolutely would be the way to go. Or you could just nest the doc string but that's admittedly less elegant.
      Is that a "bug" though? It's doing exactly what you'd expect form the code; the wrapper is being returned, not the parent function. Meh, potential undesirable / mildly inconvenient.

  • @FelLoss0
    @FelLoss0 11 місяців тому +4

    This is the clearest example I've seen on this topic. Thank you very much! Here you have a happy person that finally get over the frustrating feeling for not being able to understand this. Keep up the hard work!

  • @alexandrecuchi2400
    @alexandrecuchi2400 Рік тому +2

    Congrats on the video, it's simple and helps a lot. This functionality in python is realy useful to repetitive validations needed in functions

  • @your_teja
    @your_teja Рік тому

    These videos are simple and to the point. Helping me to build my project.

  • @glenospace
    @glenospace Рік тому +13

    You should pass kwargs with **, i e func(*args, **kwargs)

  • @Nico-eq3rf
    @Nico-eq3rf Рік тому +1

    Cool guide, learned something new. Thanks :)

  • @yogaxpto
    @yogaxpto Рік тому +6

    I would also like to see the annotations for the return type of the functions. A nice to have considering that you're annotating the arguments in the video.
    Good explanation!

    • @DrDeuteron
      @DrDeuteron 11 місяців тому

      types.MethodType or types.FunctionType (?), if you must.

  • @kevon217
    @kevon217 Рік тому

    Love your content. Succinct & informative.

  • @hikaritsumi2123
    @hikaritsumi2123 11 місяців тому +1

    Have been using this similar snippet of code for months now I have no idea what wrapper does, now I know why (and fortunately it isn't harmful!) Thank you

  • @christosmantas4308
    @christosmantas4308 10 місяців тому +1

    Nice! I didn't know about wraps

  • @markvogt70
    @markvogt70 Рік тому

    VERY USEFUL & ELEGANT video on a popular (and often MIS-used!) concept - NICELY DONE !
    - Mark in North Aurora IL USA

  • @djbroake9810
    @djbroake9810 Рік тому

    Amazing, Thank you for all the content!

    • @Indently
      @Indently  Рік тому +1

      Thank you for following along mate!

  • @prajnadeep
    @prajnadeep Рік тому +1

    This is cool. Thanks

  • @user-vb9mv9xb1x
    @user-vb9mv9xb1x 2 місяці тому

    great, thanks

  • @VaibhavSharma-zj4gk
    @VaibhavSharma-zj4gk Рік тому

    Hi nice video. Is the @wraps(func) required.

  • @nasser3780
    @nasser3780 Рік тому +9

    You're indeed the best one in Python, I've ever watch on UA-cam. I like your video where it comes alongside with full explanation, concise and more specific. I wonder how long have you been professional in Python?

    • @Indently
      @Indently  Рік тому +4

      I don't think I would call myself a professional as much as a Python enthusiast who enjoys reading the documentation ahah. I'm really happy you enjoy the videos, thanks mate! There will be plenty of more to come!

    • @jacksonbourne
      @jacksonbourne Рік тому +2

      Not to hate on this video or anything, but if you want to see other good Python tutorials check out mCoding

    • @Indently
      @Indently  Рік тому +4

      @@jacksonbourne I'm a big fan of mcoding

  • @yechielvizel
    @yechielvizel Рік тому

    Nice video

  • @12345charliebrown
    @12345charliebrown Рік тому

    How would I handle an async generator in a decorator?

  • @Denis-xl8jx
    @Denis-xl8jx Рік тому

    Nice video! What font are you using for pycharm?

  • @MarcoBarbone
    @MarcoBarbone Рік тому +2

    What if do_something returns a value? I think wrapper should do result = func(*args, *kwargs) and return result at the end

    • @EpicNoobx
      @EpicNoobx Рік тому

      Doing that will make it raise a NameError exception.
      Edit: Nevermind, I think I understand. You need to add a new return statement in the same place.

  • @AstronomyAstropy
    @AstronomyAstropy Рік тому

    I Confirm you are World's Professional Python Programmer

  • @yiyang8216
    @yiyang8216 Рік тому +1

    Quick question: What if the function do_something(param: str) has a return value? How does the decorator get_time work?

    • @grzegorzryznar5101
      @grzegorzryznar5101 Рік тому +1

      Won't work Will always return None, cause is missing return func(*args, **kwargs) inside wraper function

    • @robotsmash45
      @robotsmash45 Рік тому +1

      Save the return value when you call func() inside the wrapper() function and then return it at the end of wrapper().
      def wrapper(func):
      ....
      ret = func(*args, **kwargs)
      ...
      return ret

  • @ParkSnoopy
    @ParkSnoopy Рік тому

    Should use DOUBLE asterisk to correctly parse keyword arguments into keyword arguments, if you use one asterisk, it will parse into the positional argument.
    And to make sure wrapped function's return returns its result properly, should save the return of parameter (inner) function and return it from inner function again.

    • @Indently
      @Indently  Рік тому

      You're absolutely right, I honestly didn't see that I only wrote 1 until now

  • @markhoo
    @markhoo 8 місяців тому

    cool

  • @pacersgo
    @pacersgo Рік тому +3

    Nice contents. What is missing is how to pass parameters to the decorators

    • @Indently
      @Indently  Рік тому +5

      You're right, I missed that! I'll make another video in the near future covering that.

    • @preritdas6998
      @preritdas6998 Рік тому +1

      Yes, I’d like to see this too, the only way I know right now is by using yet another layer of wrapper, so you basically have two nested functions. You have a function that takes your special args that creates a decorator that takes the function that creates the wrapper that takes args and kwargs…..

    • @Indently
      @Indently  Рік тому +2

      @@preritdas6998 I will look into it, but this sounds like the only approach I know of at the time being.

    • @gabi_kun3455
      @gabi_kun3455 Рік тому

      You can put any expression as a decorator,
      That means you can put a function call that returns the decorator:
      @ === @ === @function() -> @returned_decorator === used as decorator,
      But when you do simply:
      @ === @ === used as decorator

  • @CrapE_DM
    @CrapE_DM Рік тому +30

    Not a bad video, but I don't see why the 2022 modifier had to be added in the video. It implies there's something new to this, but wraps() has been around for quite some time.

    • @Indently
      @Indently  Рік тому +23

      It's common to include the year in a programming video to show that the information is still relevant in that current year. Thanks for the comment!

    • @guinea_horn
      @guinea_horn Рік тому +1

      @@Indently it's not common, this is the first and only time I've seen this

    • @dogyX3
      @dogyX3 Рік тому +2

      ​@@Indentlyfair enough. I've seen this on quite a few videos myself

    • @kie9683
      @kie9683 Рік тому +1

      @@guinea_horn you must never watch any videos about coding, game development, or api interactions ever.

  • @jkjohnsonofficials
    @jkjohnsonofficials Рік тому

    1st view Bro

  • @hacked.hacked
    @hacked.hacked Рік тому +1

    ❤️

  • @MaxymYT
    @MaxymYT Рік тому

    I get "NoneType object is not callable" error, why is that?

    • @DrDeuteron
      @DrDeuteron 11 місяців тому

      did you remember to return the wrapped function from the decorator?

  • @AWriterWandering
    @AWriterWandering Рік тому

    There’s something almost meta about using a wrapper to fix wrappers.

    • @Indently
      @Indently  Рік тому +1

      I'm not so impressed with that either ahah, there's probably a good reason for it (maybe there isn't), I'll have to look deeper into the documentation for that.

  • @Mingming_Studio
    @Mingming_Studio Рік тому +1

    There's something I don't get it.
    1. what is "*args" and "**kwargs" ? what does "*" mean?
    2. Why can't we just put that timer function code in the function "get_time", but define a function called "wrapper", and then "return wrapper"?

    • @tfr
      @tfr Рік тому +2

      "args" and "kwargs" is just a name. programatically, "*" (in a function definition) means ARGS for the interpreter, additionally ** means KWARGS
      it has just become standardized to use args and kwargs as their names but
      def myFunc(*a, **b)
      would work too, just its a bit non standardized.

  • @horoshuhin
    @horoshuhin Рік тому

    And what's a wrong way then. ?

  • @klaasvaak2575
    @klaasvaak2575 Рік тому

    instructions unclear, year is 2023 and my computer exploded :P

  • @idkthischannelwhichcametom6784

    Man, you serious? This was already added to python...thanks for revision btw.

  • @jackdumanat49
    @jackdumanat49 Рік тому

    for the longest time i've been wondering what decorators are for. Been watching a lot of videos and this is the one that made it click for me. right right I kinda understand why dash uses @callback or @app.callback now.

  • @knut-olaihelgesen3608
    @knut-olaihelgesen3608 Рік тому

    functools.wraps does noe prevent a bug, because there was never a bug on the first place. Wrong choice of words