Expert Python Tutorial #4 - Decorators

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 ก.พ. 2025

ความคิดเห็น • 128

  • @deadweightyeti7002
    @deadweightyeti7002 2 ปีที่แล้ว +16

    I've been wanting to reach the next level but documentation is too confusing. These videos have been helping me a ton!

    • @TechWithTim
      @TechWithTim  2 ปีที่แล้ว +2

      Thanks so much for the donation!

  • @VictorCaldo
    @VictorCaldo 4 ปีที่แล้ว +21

    I think I can finally incorporate them into my code! As a self-taught Biotech your content does all the difference, thank you!

  • @dato007
    @dato007 ปีที่แล้ว

    The best on the internet. I’m serious. You are precise, concise, yet still capture the relevant nuance. Never stop!

  • @blakebiz2335
    @blakebiz2335 5 ปีที่แล้ว +15

    This is by far my favorite series from you :) this covers exactly what I’ve been meaning to learn but just haven’t gotten around to in python! Thanks so much!

  • @erickalvarez6486
    @erickalvarez6486 5 ปีที่แล้ว +28

    Geez never thought I'd learn this much from a TH-cam video, did not regret of subscribing, thanks a lot, now I understand more

  • @chethancheths4198
    @chethancheths4198 4 ปีที่แล้ว +18

    The best tutorial for getting into expert zone
    Kindly Dont miss to teach us about threading ♥️

  • @dev_soda8315
    @dev_soda8315 2 ปีที่แล้ว +2

    I love your teaching style. It is at such a level that it is easy to follow, while still being able to teach all the content. Your channel is a prime example of what a good tutorial channel should be like.

  • @anuragagarwal4680
    @anuragagarwal4680 3 ปีที่แล้ว

    The way @Tim explians all the stuff, thats incredible.

  • @rickyofficial5498
    @rickyofficial5498 3 ปีที่แล้ว +3

    Was searching for hours and hours for a perfect tutorial and this was the best!

  • @zbmadon
    @zbmadon 3 ปีที่แล้ว

    Best tutorial explaining this concept. It makes such a big difference by keeping it simple and using print statements, instead of making nonsensical convoluted stuff that I've seen other tutorials do.

  • @sciencedoneright
    @sciencedoneright 3 ปีที่แล้ว +12

    Dude I feel like a python god when I learn these expert level stuff

  • @hackerzone2002
    @hackerzone2002 5 หลายเดือนก่อน

    Really it was amazing Video I was very confused in decorator after watching your video my all concepts got cleared and not about decorators i have watched your most of the lecture and the only thing which i wan't to say is all are amazing the content you provide in your video is easy to digestible . Thank you tim ☺

  • @Lofwyrf
    @Lofwyrf 4 ปีที่แล้ว +6

    This was so uuuuuuuuussssssssssssssefull thanks a lot. Never understood what Decoraters actualy did.

  • @a4e69636b
    @a4e69636b 3 ปีที่แล้ว

    At first I thought decorators were a mistake, now I see they can be useful. Thank you for this video.

  • @mahmoudelleithy6908
    @mahmoudelleithy6908 11 หลายเดือนก่อน +1

    dont like write comments much but this one i couldn't skip not to say thank you very concise

  • @tramphuong5231
    @tramphuong5231 3 ปีที่แล้ว +1

    You always explain thing at their root! That’s why my mind loves to understand it. Thanks so much!! 😊

  • @Oscar-vs5yw
    @Oscar-vs5yw 2 ปีที่แล้ว +2

    This series is really great, covers all the stuff I never bothered to learn 😢😂

  • @emeraldthunder
    @emeraldthunder 3 ปีที่แล้ว

    Thank you. I've been using them in my latest project, a discord bot, and I haven't really though about how they work instead just taking them for granted. Now I can use them in more of my projects to hopefully speed up my code a little bit.

  • @dany_fg
    @dany_fg 8 หลายเดือนก่อน +1

    It's extremely useful for exception catching, especially for testing (the decoratir will return test successful or failed)

  • @willet2275
    @willet2275 5 ปีที่แล้ว +12

    Instead of showing the program syntax - I think the best tutorial is showing real, practical application programs. Variable definitions and function definitions can be found in every manual.
    What is useful, is to show the overall structure of the program: how the program is built depending on its purpose, and what part of it is responsible for. From General to specific.
    It's like learning a foreign language from a boring alphabet. There are plenty of such tutorials.
    And yet to learn a language, you can start to play specific genre scenes from real life.

  • @prajp13
    @prajp13 4 ปีที่แล้ว +1

    hi tim, thanks a lot for this video , decorators concept is so clear now , best explanation, this is my first ever comment on youtube

  • @anumalik9555
    @anumalik9555 2 ปีที่แล้ว

    Thank you, it's amazing tutorial... Never ever seen a tutorial like this.. so so good, covers all the concepts. Thank you once again :)

  • @rahultirkey7950
    @rahultirkey7950 5 ปีที่แล้ว +1

    Learned a lot of advanced stuff from your tutorial. Keep making more tutorials.

  • @ipfinder8919
    @ipfinder8919 3 ปีที่แล้ว +1

    Tim, you're a great teacher. Thanks a lot.

  • @impos1ble32
    @impos1ble32 5 ปีที่แล้ว +2

    Awesome video, learned a lot. surprised that you did it almost perfectly in one take!

  • @Rwpankaj
    @Rwpankaj 4 ปีที่แล้ว

    one of the best explanation of confusing concepts like metaclasses and decorators. Thanks for awesome videos !!! :)

  • @SkyFly19853
    @SkyFly19853 5 ปีที่แล้ว +1

    One of the most informative tutorials!
    💯💯💯💯💯💯

  • @STFU665
    @STFU665 2 ปีที่แล้ว

    Realy like your videos and i don't want to be the old guy here, but mainly this is C pointers in "not so ugly" mode. There are good books on this and they really help understanding the python way on getting things done.

  • @adthn
    @adthn 4 ปีที่แล้ว +1

    Hey nice tutorial😊subscribed🤍
    And also which IDE r u using? Is it available in Windows?

  • @Houndfederation
    @Houndfederation 3 ปีที่แล้ว +1

    Loving the title "Expert Python Tutorial - Decorators". Everytime I see "Python tutorial FOR ABSOLUTE BEGINNERS" combined with something like dunder methods or decorators/getter/setter I feel like this programming thing might not be for me :D
    This at least does not make me feel like I'm completely stupid :D

  • @anidiotsguide757
    @anidiotsguide757 ปีที่แล้ว

    Tim: "This is advanced Python"
    My teacher: "It's the third week of this python introduction course, let's talka about decorators!"

  • @sacredinclinations
    @sacredinclinations 4 ปีที่แล้ว

    Excellent to see your use cases here of decorators made it easy to understand because you linked to stuff covered in other video about passing a function as an argument.
    Question for you if you see this: how would you iterate over attributes of a class instance?

  • @blastav
    @blastav 2 ปีที่แล้ว

    It’s one of the best explanation of decorators. Thank you!
    Also thank you for example.
    It would be great if you will show more examples. Logging for example

  • @bahibrahim101
    @bahibrahim101 ปีที่แล้ว

    A treasure trove. Ty Tim.

  • @en8042
    @en8042 4 ปีที่แล้ว +1

    Hey Tim, just one question. What's the point of creating the wrapper function? It feels like an unnecessary step. The first example could just go without it. The problem occurs when arguments are passed to the function, but why? Can you explain please, thanks.

  • @hectorminator4
    @hectorminator4 3 ปีที่แล้ว +3

    Thanks a lot for your videos, you explain quite simple and user friendly, appreciate your work man, but there is a doubt I have
    what is the use of the wrapper functions?? Why don't just define operations of the function inside the main function?? Makes no sense to an amateur like me...

  • @ahmed.rosanally
    @ahmed.rosanally 5 ปีที่แล้ว +1

    Saw decorators being used the pizza API code you've used in a previous vid series. Really interesting to see how it works, especially with *args and **kwargs

  • @migueldomingos4570
    @migueldomingos4570 5 ปีที่แล้ว +1

    I love this tuts! What other expert python tuts will you make?

  • @georgettebeulah4427
    @georgettebeulah4427 4 ปีที่แล้ว +2

    This make so much sense and meaning

  • @micah2936
    @micah2936 2 ปีที่แล้ว +1

    Step one write some partially copied code
    Step two, see Tim to figure out why your code works

  • @jonathanmoore5619
    @jonathanmoore5619 5 หลายเดือนก่อน

    Beautiful explanation. Thanks

  • @zavarkozoltan5917
    @zavarkozoltan5917 2 ปีที่แล้ว

    Just a question, why are you storing the return value (rv) in the last example? i cannot understand that part. You can just call the func, unless you wanted to make this last example universal? In case we will have some functions later where we return value?

  • @iulianmatei7336
    @iulianmatei7336 5 ปีที่แล้ว +1

    I am not gonna say that i am first , even that i am . But , serriosly , thank u Tim 🙏🙏

  • @chukwukodinakaugochukwu2817
    @chukwukodinakaugochukwu2817 2 ปีที่แล้ว

    Love your teachings bro

  • @TheDima23
    @TheDima23 3 ปีที่แล้ว

    Thank you for this you cleared so many issues I had

  • @leumas_tai
    @leumas_tai 2 ปีที่แล้ว

    Nice one. You've got a new subscriber

  • @lmagiczlukas4891
    @lmagiczlukas4891 4 ปีที่แล้ว

    I always wait for a new video in this series.

  • @tn5420
    @tn5420 4 ปีที่แล้ว +10

    Listen to his voice at 12:24 haaaaaa. Good tutorial though thanks man!

    • @sebaperalta2001
      @sebaperalta2001 4 ปีที่แล้ว +4

      I can't unlisten it now lol

    • @inx1819
      @inx1819 4 ปีที่แล้ว

      what you could dO

  • @FUNG36987
    @FUNG36987 4 ปีที่แล้ว +6

    you didn't mention the implementation on decorators having their own parameters which in some cases are more generic and flexible

  • @baphnie
    @baphnie 2 ปีที่แล้ว

    omg I get it now. The implicit passing of the decorated function object as a parameter kept throwing me.

  • @stivunnikolov5941
    @stivunnikolov5941 3 ปีที่แล้ว

    This is kind of complicated explanation but greetings for taking the time.

  • @andreacodutti1313
    @andreacodutti1313 5 ปีที่แล้ว +5

    Another useful application is memoizing recursive function calls, for example for Fibonacci sequence

  • @debojyotichatterjee4421
    @debojyotichatterjee4421 3 ปีที่แล้ว

    Really amazing thanks a lot... a clear understanding for me!!

  • @beatpapes
    @beatpapes 4 ปีที่แล้ว

    Now that I've seen the decorator function used as a time an execution of the function. One of the things I can think of that could be useful for using decorators are unit tests, is it correct?
    We can write unit tests for functions so we want to cover the major outcomes of the function and if they work as expected.

  • @DrDeuteron
    @DrDeuteron 2 ปีที่แล้ว

    You can also decorate classes, which may let you avoid using dunder metaclass.

  • @rosesofficialhusband5728
    @rosesofficialhusband5728 ปีที่แล้ว

    What IDE is this? It looks good

  • @rolandogarcia3230
    @rolandogarcia3230 5 ปีที่แล้ว

    Hey bro. Thanks for your tutorials. You are the best

  • @felipica
    @felipica 2 ปีที่แล้ว

    acho q entendi decorators finalmente valeu demais mano ajudou mt tmj

  • @Ihsan_almohsin
    @Ihsan_almohsin 3 ปีที่แล้ว

    well explained

  • @kazifaisal-rj9js
    @kazifaisal-rj9js 2 ปีที่แล้ว

    Yoda !!! Thanks, really appreciate it

  • @Xaminn
    @Xaminn 3 ปีที่แล้ว

    So many Funcs, I'm about play that music.

  • @donatellodonini3147
    @donatellodonini3147 3 ปีที่แล้ว

    I need an answer: does the wrapper function exist because either way the decorating function would be called every time?

    • @donatellodonini3147
      @donatellodonini3147 3 ปีที่แล้ว

      Than i have another question: how do i interact with the arguments that are passed in the decorated function?

  • @aim2986
    @aim2986 3 ปีที่แล้ว

    0:13 actually they CAN change code of the function but generally no one needs to do that

  • @thedevhassan
    @thedevhassan 5 ปีที่แล้ว +2

    Kindly make a full course on Django plz

  • @BrendanMetcalfe
    @BrendanMetcalfe 5 ปีที่แล้ว

    Great explanation!

  • @mohammedfareedh
    @mohammedfareedh 5 ปีที่แล้ว

    *Thanks Tim*

  • @lilagraham6481
    @lilagraham6481 3 ปีที่แล้ว

    So, I tried running the "func(f) code without wrapper() inside, just: print("Started"), f(), print("Ended"), return. All the rest was the same and it ran perfectly, giving the same output, except after printing the output it gave an error saying "func3() ... Nonetype object is not callable." And yet I called it. And it ran fine. Why bother to throw an error at me after it already executed exactly as it should have?

  • @hakanhakan6267
    @hakanhakan6267 9 หลายเดือนก่อน

    do you have the code that you use in your videos ?

  • @spheenik
    @spheenik 4 ปีที่แล้ว +1

    Nice tutorial, but it would have been clearer if you called func wrap instead and maybe func2 foo and func3 bar. That way it would have been x = wrap(foo) which is much clearer.

  • @pawjast
    @pawjast 2 ปีที่แล้ว

    Good explanation.

  • @bahibrahim101
    @bahibrahim101 ปีที่แล้ว

    An implicit intro to aspect oriented programming

  • @nickwu5317
    @nickwu5317 4 ปีที่แล้ว

    learning by coding, great !

  • @dagoporrasplata454
    @dagoporrasplata454 4 ปีที่แล้ว

    how it works when you apply 2 or more decorators? I mean, python applies decorators in some order?

  • @cesararacena
    @cesararacena 2 ปีที่แล้ว

    Nice videos. Found your channel not long ago and that's why the late comment. Unfortunately, for everyone using this python-included way of writing decorators, the function being wrapped looses its properties to the wrapper function. If you print for example the __name__ of the function being wrapped (i.e. with print(func2.__name__)) you will notice the name is "wrapper" instead of func2. In most cases this shouldn't make a difference for us but if you really want you can change this just a little bit and use a function included in functools called wraps (actually it is a decorator itself so you decorate your wrapper function with @wraps) and the function being decorated will retain its properties. Here's a simple example using the same logic as Tim:
    def func1(f):
    @wraps(f)
    def wrap(*args, **kwargs):
    print('begin...')
    rv = f(*args, **kwargs)
    print('end...')
    return rv
    return wrap
    @func1
    def my_func1(x, y):
    print(x)
    return y
    x = my_func1(5, 6)
    print(x)
    print(my_func1.__name__)
    Hope it helps somebody. Cheers!

  • @yeyintaung9837
    @yeyintaung9837 ปีที่แล้ว

    i can't thank you enough bro

  • @vivekr.k7950
    @vivekr.k7950 2 ปีที่แล้ว

    Nice explanation

  • @sean_hare
    @sean_hare 4 ปีที่แล้ว

    Top notch!

  • @hutsons-hacks3668
    @hutsons-hacks3668 3 ปีที่แล้ว

    Very nice 👌

  • @lifeisnotvibestv5124
    @lifeisnotvibestv5124 5 ปีที่แล้ว

    I was wondering where you at. Thanks Tim

  • @jijie133
    @jijie133 ปีที่แล้ว

    Great video.

  • @scorpp149
    @scorpp149 4 ปีที่แล้ว

    Please i need an answer, i don't know what to type in google : when you store the return value in a variable and then return it, why does the prints show up between the "started" and "ended" strings?

    • @loganrowe1727
      @loganrowe1727 4 ปีที่แล้ว +1

      It is because there is a print statement inside the function that he is wrapping. So f() is run (which then prints a value) and it's return value is assigned to rv between "started" and "ended". The only thing that happens after "ended" is that rv is assigned to x.

    • @scorpp149
      @scorpp149 4 ปีที่แล้ว

      @@loganrowe1727 wonderful thx

  • @sidnrock1165
    @sidnrock1165 4 ปีที่แล้ว

    Well Articulated.

  • @AK-fr8sy
    @AK-fr8sy 2 ปีที่แล้ว

    Legendary

  • @Just_Do_It_x
    @Just_Do_It_x 3 ปีที่แล้ว

    Nailed it

  • @RoboMarchello
    @RoboMarchello 4 ปีที่แล้ว

    thank you!

  • @liri1189
    @liri1189 2 ปีที่แล้ว

    I don't get the part of return wrapper, can anyone explain please?

  • @nikolastamenkovic7069
    @nikolastamenkovic7069 2 ปีที่แล้ว

    Good one

  • @mouaadrio5883
    @mouaadrio5883 3 ปีที่แล้ว

    what font is he using please ??

  • @MrNolimitech
    @MrNolimitech 2 ปีที่แล้ว

    Great video. but it missed something important. The decorator @wraps from functools. To keep track of the information about the REAL function itself, not the decorator.
    ```
    from functools import wraps
    def timer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
    .....
    ```

  • @theneongamer4957
    @theneongamer4957 5 ปีที่แล้ว +3

    Great series please continue, I want to ask you a qeustion about what laptop/pc do u use and what is the CPU and how many cores it has. Also I have a problem with flask when I try to write @app.route("/")
    def home():
    return render_template("index1.html")
    when I run it it displays "jinja2.exceptions.TemplateNotFound: index1.html"(with debugger on) so if u have any knowledge about this please tell me

    • @TechWithTim
      @TechWithTim  5 ปีที่แล้ว

      i have flask tutorials on how to render templates try watching those.

  • @mohamedfaresbenayed9156
    @mohamedfaresbenayed9156 3 ปีที่แล้ว

    How about passing an argument to a decorator?

  • @itssuperbaby2979
    @itssuperbaby2979 3 ปีที่แล้ว +1

    12:24 the voice crack-

  • @ahmed.rosanally
    @ahmed.rosanally 5 ปีที่แล้ว

    You should elaborate more on how *args and **kwargs are used in a future python expert video

    • @ezraarhin5312
      @ezraarhin5312 5 ปีที่แล้ว

      **kwargs

    • @ahmed.rosanally
      @ahmed.rosanally 4 ปีที่แล้ว

      they remind me of pointers in C, although they behave different conceptually

  • @travismiller3636
    @travismiller3636 2 ปีที่แล้ว

    My man streaming Python tutorials from a Five Guys bathroom.

  • @nujranujranujra
    @nujranujranujra 2 ปีที่แล้ว

    noorandu kaalam vazhga!

  • @sergioapreda9270
    @sergioapreda9270 4 ปีที่แล้ว

    Personally, this does not seems on par with the rest of your material. A lot more could've been said (decorator with arguments, multiple decorators resolution order, class decorators, ....etc). Example-wise the timer is fine, but maybe a little bit over-simplistic, it is an expert tutorial after all. That said, i really appreciate all the effort you put in your videos, and found your material very useful in many occasions, so keep up the good work!
    .

    • @TechWithTim
      @TechWithTim  4 ปีที่แล้ว +1

      Appreciate the feedback!!

    • @sergioapreda9270
      @sergioapreda9270 4 ปีที่แล้ว

      @@TechWithTim maybe a follow-up, in-depth, video? Expanding on some topics and perhaps showing some "real-world" application use-cases. Flask and Click comes to mind (They do extensive use of decorators and could be nice to tie them up in a single project) :)

  • @retnikt1666
    @retnikt1666 4 ปีที่แล้ว

    do the descriptor protocol next

  • @vebexplaeya
    @vebexplaeya 5 ปีที่แล้ว +3

    Please Sockets in Python, Please Tim 🙂🙃

    • @Ace44rto
      @Ace44rto 4 ปีที่แล้ว +1

      Look at his online game pygame it uses sockets and threading

    • @westernvibes1267
      @westernvibes1267 4 ปีที่แล้ว

      Import socket
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect (("ipaddress", port))
      s.recv(1024)
      s.send("fuckyou")
      s.close()
      Here you go socket programming

    • @vebexplaeya
      @vebexplaeya 4 ปีที่แล้ว +1

      @@westernvibes1267 what about handling multiple clients 🤔

    • @ekrem_dincel
      @ekrem_dincel 4 ปีที่แล้ว

      @@vebexplaeya copy pasta

  • @tiga1662
    @tiga1662 5 ปีที่แล้ว

    Full Django course please ;)

  • @vsevolodzakharov4501
    @vsevolodzakharov4501 2 ปีที่แล้ว

    It looks like class decorators are missing