PLEASE Use These 5 Python Decorators

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

КОМЕНТАРІ • 87

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

    To learn programming and Python - check out Datacamp!
    💻 Learn Python - datacamp.pxf.io/4PODM0
    💻 Learn Programming - datacamp.pxf.io/3e6xzr

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

      Hello, sorry to put this comment here but i need help with my flask app, im trying to put my app in an Ubuntu server but im using plesk, i already followed your instructions in your video about It but It doesnt work, this is my first website so im clueless and i would really appreciate some help

  • @kameroongod
    @kameroongod 10 місяців тому +79

    The more I learn the more I realize I know nothing lol. Keep up the content. Few channels that gets me listening to tutorial videos in my spare time just for fun lol.

    • @franco-gil
      @franco-gil 10 місяців тому

      The more you learn, the more you can assist others, keep learning new things. A secret, replicate all the content you are trying to learn, you will understand in a more easy way.

    • @GenX_Tech
      @GenX_Tech 6 місяців тому

      Same here for the most part.

    • @BMM-op4ou
      @BMM-op4ou 2 місяці тому

      Even if you have 20 years exp in coding, you will still have more to learn coz these things keep on evolving. The key is to know the essentials, anything else you can google. Keep safe🎉

    • @spandanganguli6903
      @spandanganguli6903 Місяць тому

      Dunning Kruger effect. You will never again have the same confidence about this subject as you did as a beginner, even if you become an expert without equal.

  • @LolkeDijkstra
    @LolkeDijkstra 6 місяців тому +5

    Excellent stuff. What I particularly like about the @property is that if you start without and then refactor using @property, it doesn't break your code. You just need to prepend _ to the property and add the getter and setter.

  • @alimihakeem841
    @alimihakeem841 10 місяців тому +4

    I love your content Tim. Keep it up. I have really learnt alot from you. I see as you as my role model

  • @Dot_Starshot
    @Dot_Starshot 10 місяців тому +5

    So excited to watch this!!

  • @neurooted
    @neurooted 9 місяців тому +1

    Keep it up Tim! Very very high quality video.

  • @JorgeEscobarMX
    @JorgeEscobarMX 7 місяців тому +1

    This is an excellent video about decorators, I never created my own data classes with methods, but I do use the @dataclass decorator and never knew all what comes with it.

  • @amisamaira194
    @amisamaira194 Місяць тому

    I never needed to use decorator before. so I never understood how it works, until today. Glad I found this video.

  • @mattmarshall1834
    @mattmarshall1834 9 місяців тому +2

    The functools cache speeding up the Fibonacci func is crazy. I know how it works but it's still crazy to see.

  • @prannoyroy5312
    @prannoyroy5312 Місяць тому

    Awesome video!! Everything single thing was very enlightening and added to our knowledge. Thank you!

  • @samukaze5810
    @samukaze5810 10 місяців тому +2

    Tech With Tim I love your informative videos!

  • @garfield-pro
    @garfield-pro 10 місяців тому +1

    9:31 What would happen if we try to assign some value to c.diameter?

    •  9 місяців тому

      Error - no setter defined.

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

      and if you do have the bad habit of assigning attributes outside of dunder init, you can use the setter to flag when it happens, so you can find the inevitable bug caused by that bad habit.

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

    Why do we use decorator? @4:31 mark? We could simply write another function and call the example function from within it. Add start_time and end_time above and below the function call to example function. Thatbwould be sufficient, isnit not so?

    • @rupen42
      @rupen42 8 місяців тому +3

      Yes, that is sufficient _in this case_. But what if you want to time 5 functions? 20 functions? 100 functions? You can write 1 decorator and apply it to them all or write n individual functions for all of them.

    • @kristofferjohansson3768
      @kristofferjohansson3768 6 місяців тому

      @@rupen42 @willk4481 And you can apply multiple decorators to the same function. It is clearer than chaining multiple functions.

  • @BobbyConquest
    @BobbyConquest 9 місяців тому

    15:41 how do you type/ make those lines in the comment? Super clean. Love all your videos Tim, you have a gift for teaching

  • @grmrcy
    @grmrcy 10 місяців тому +2

    Loved the video, but with all due respect: there is an error in the Fibonacci code. For initial conditions it should be if n < 2, return 1 (not n, as it will return 0 for n=0, but fibonacci(0) should be 1)

    • @sri_harsha_dv
      @sri_harsha_dv 10 місяців тому +2

      It all depends on how you want to start the fibonacci series. Series can either start with (0,1) or (1,1) or (1,2), all of them lead to same sequence starting at different positions. And the time complexity barely changes.

  • @JamesPancoast
    @JamesPancoast 6 місяців тому

    This is a great explanation of what Decorators do

  • @sildistruttore
    @sildistruttore 7 місяців тому

    is the dataclass decorator making impossible to use "global" variable inside a class how you showed using the classmethod decorator?

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

    i loved this one of datacleasses because writing __init__ every time was boring, but does it work on super classes?

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

    Thank you very much! Truly enlightening!

  • @engineerprototype9191
    @engineerprototype9191 6 місяців тому

    very clear and concise

  • @daven616
    @daven616 Місяць тому

    My background is embedded systems but I know Python. I nearly wretched when he suggested using a cache decorator to make a recursive Fibonacci function more efficient. There is absolutely no reason except educational to write a recursive Fibonacci function. The iterative one is fast, memory efficient, and more clear. Try computing the millionth Fibonacci number recursively. Programmers need to know what is happening under the hood. Here, no recursion, no caching. Clear and fast. Below generates the entire sequence up to n, but can easily be modified to return the nth as an integer.
    In my first coding job interview I was asked to write the recursive Fibonacci function and then asked why it was a bad idea. Don't use caching to make it faster, just don't use recursion! It is even crazier to call it with n=1, 2, 3, 4, etc. to generate the sequence. It is as useful example of recursion but should not be used in any performance critical application. Recursion is essential for many problems, like tree traversal, depth first search, but not for simple math like the Fibonacci sequence.
    def fibonacci(n:int) -> list:
    if n

  • @Musaafir-ln6feet
    @Musaafir-ln6feet 10 місяців тому

    Nice can you also cover descriptors and Enums in detail they improve on dataclass.

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

    I like functool's total_ordering decorator.

  • @zimzimph
    @zimzimph 3 місяці тому

    Would've been nice to see the @timer decorator with the fibonacci example

  • @GasterLab
    @GasterLab 7 місяців тому

    I wouldn't say that using @dataclass decorator benefits in python coding. Because code generated by using exec() not cached where dataclass is using this by default. So, because of that. If there are a lot of dataclasses python programm will load slower

  • @franco-gil
    @franco-gil 10 місяців тому

    [9:02] zero is not a positive number, it is a neutral number, you should improve the ‘if’ statement, nice video 👋

  • @Dd-do-and-dont
    @Dd-do-and-dont 9 місяців тому

    0:17 function, method or class

  • @cfwebdeveloper
    @cfwebdeveloper 10 місяців тому

    May we use setter/getter in django? Probably yes? If using models can this be used? Maybe as custom validators probably? My brain just wrote this out lol but seems feasible?

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

      what do you mean getters/setter? property and property().setter, yes, but if you're writing methods called set_radius(self, radius) or get_radius(self), then no. Never. It's not java.

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

      @@DrDeuteron Totally agree. I also don't touch Java, ever lol

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

      omg, I was on a NASA code project and it was full of:
      space_craft . get_intrument() . get_sensor . get_channel() . get_shelves() . get_shelf()
      . get_temperature()
      (and setters like that!)
      calls, and the 1st thing I did was turn it all in to @property and dunder getattr / init magic, (they were doing bare init class with post attribute setters), oh, and they had one class per module...so they came in Monday freaking out "85% of the code base is gone!"
      I raised my hand and said "you're welcome".
      It was pretty rocky after that ;-)

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

      @@DrDeuteron jesus lol

  • @iuritorres
    @iuritorres 9 місяців тому +1

    someone can give me more examples of when dataclass will be more useful than normal classes?

    • @DrDeuteron
      @DrDeuteron 8 місяців тому +2

      for me, when I'm reading 545 data fields of space craft telemetry, which is a helluva dunder init. Instead I just cut and paste the attributes from an excel file into a data class and it calls for a single mouse click. Moreover, dataclass comes with "fields", which can be used to describe the fields, the LaTex formulas for them. expected ranges, alarm ranges, broke ranges, and there is an astuple functions, which can be stuck in dunder iter, possible zip with the fields, and in about 3 lines of executable code, you can flag out-of-range telemetry. Very little dynamic code, and lots of static could is much simpler and less bug prone.

  • @Al_Miqdad_
    @Al_Miqdad_ 10 місяців тому

    cool information
    thank you
    ❤❤

  • @dungam9402
    @dungam9402 4 місяці тому

    follow him when he was young till now

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

    Nice video bro 😊

  • @abdallahbabeker3324
    @abdallahbabeker3324 10 місяців тому

    Thanks for the video ❤ what's the name of your theme in vs code

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

    what is the theme name please?

  • @StoicismAspects
    @StoicismAspects 10 місяців тому

    Thank you bro

  • @sanukyadav
    @sanukyadav 9 місяців тому +4

    7.18 using '_' single underscore represents protected item not private, "__" this is private.

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

    Awsome 🎉🎉❤

  • @ataru-music
    @ataru-music 6 місяців тому

    I find using the recursive function in the Fibonacci algorithm totally inefficient. Even if there is an improvement in performance with cache, it is still a bad way to implement that type of algorithm.

  • @StoicismAspects
    @StoicismAspects 10 місяців тому

    plz make a pygame full course video

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

    Ok, we've got python decorators - but when are we getting python painters?

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

    Tim is da python expert. I'm afraid of pythons.

  • @davidlu1003
    @davidlu1003 2 місяці тому

    thx,bro😁😁😁

  • @kralbalkizar
    @kralbalkizar 10 місяців тому

    This cache decorator!

  • @calvintran8691
    @calvintran8691 10 днів тому

    Please explain *args and **kwargs

  • @Ollital
    @Ollital 3 місяці тому

    I'm a big fan of the Fibonacci function.

  • @monishbiswas1966
    @monishbiswas1966 9 місяців тому

    Of course you’d never write a recursive Fibonacci function like that - you’d get it to return the last two numbers so you only need one recursive call.

  • @philtoa334
    @philtoa334 10 місяців тому

  • @GenX_Tech
    @GenX_Tech 6 місяців тому

    Why not just write the function to display the way you want it to? Docorators seem like extra nonsense to me.

  • @hiedtchannel
    @hiedtchannel 10 місяців тому +3

    Nice video. It enhances my belief that freshers should start with C++ or Java. Understanding the decorator, design patterns, static vs class methods, access control, and other OOP concepts makes people write better Python code. Starting with Python should only work for non-IT who need to function quickly.

    • @blazkowicz666
      @blazkowicz666 9 місяців тому

      You are right. I feel in school or college, people should really try to learn:
      C to learn how a computer actually works
      Java to really understand OOP, Generics, Reflection etc.
      And C++ to implement concepts learnt from C and Java to create performant software

  • @salarbahador1270
    @salarbahador1270 10 місяців тому +2

    Which programming language doesn't support access modifiers these days? Do you think a convention stops people from wrongly using the methods or properties? Besides Python also doesn't support Interfaces. I'm not sure what's all the fuss about. Even modern PHP is more standard than Python; which people advertise PHP is dead.

    • @rupen42
      @rupen42 8 місяців тому +3

      Python is a language for consenting adults. Not sure what you mean by not supporting interfaces, since untyped Python obviously doesn't have them, and type-annotated Python definitely has them (protocols). There are also abcs (abstract base classes) that serve a similar function _and_ are checked at runtime.

  • @pokerchannel6991
    @pokerchannel6991 3 місяці тому +1

    decorator? More like usurper. It gets its tentacles around your function and completely takes over. And the original function is at the complete mercy of the take over, mind control of the supposed "decorator." A decorator decorates the way a soul body snatcher comes to 'accompany' your soul. Don't let them in the door, lest your function will never see the light of day, dude. Don't get fooled. Decorator is a misnomer. Call it for what it is: top to bottom inside to out infestationer.

    • @pokerchannel6991
      @pokerchannel6991 Місяць тому +1

      @@Colacoca187 I am trying to help you understand the tool by naming it correctly. Soul-snather. If you make a function, and a soul-snatcher (misnamed as decorator), it takes over every facet of your function. Is that the fate you want for you hard worked function, that you created? No. You are welcome. You WILL thank me. You will. And you will do it now.

  • @hasithkashyapa7645
    @hasithkashyapa7645 10 місяців тому +2

    Ok i saw the same thing few days ago. So this is basically a copy.

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

    So you implement privatization schema to 'fix' the deficiency of Python in having all public attributes and methods. Also adding 'hints' to variable definitions to make up for the type-agnostic nature of the language. Next "awesome best language ever" coming soon to a computer near you!

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

      Not really sure what you're point is. The main use case of the property abstraction of a private variable is for libraries. No reason for actual strict access control. Also type hints are mostly for runtime type checking and editors.

  • @Smallz2000i
    @Smallz2000i 10 місяців тому

    slowprint lol

  • @z.8477
    @z.8477 10 місяців тому +1

    PLEASE DO NOT Use These ANKWARD FACES

  • @Al_Miqdad_
    @Al_Miqdad_ 10 місяців тому

    can you give me your course for free and I'll pay after I get job please ?
    I don't have a job

  • @publicuser775
    @publicuser775 10 місяців тому

    Hate this click bites

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

    Please don’t use python

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

    Just googled !r, it returns the repr in an f-string! And !s will give you dunder str. I had no idea this was a thing!
    Learned something new today.