What are Decorators in Python? 2MinutesPy

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ก.พ. 2024
  • #decorators #decorator #decoratorinpython #python #pythonprogramming #2minutespy
    🖐Hey, want to know about decorators in Python?
    🐍 In this video, we're covering a powerful topic "Decorators in Python" - a super handy tool to add extra functionality to the functions or methods without directly changing their source code.
    What are decorators and how to use decorators in Python programs - all these questions are demystified in this video.
    More 2 Minutes Python Tutorial:
    List comprehension in Python: • How to use list compre...
    How to use map() in Python: • How to use map() funct...
    reverse() vs. reversed() in Python: • Python's reverse() Vs ...
    Why Flask(__name__) is Used When Creating a Flask App? • Why Flask(__name__) is...
    What is Global Interpreter Lock in Python? • Global Interpreter Loc...
    What is a Lambda Function in Python? • What is a Lambda Funct...
    Why __init__.py File is Used in Python Projects: • Why __init__.py File i...
    if _name_ == "__main__" in Python: • What if __name__ == '_...
    Python's seek() and tell() functions in 2 Minutes: • Understanding Python's...
    - Python Threading in 2 Minutes: • Python Threading in 2 ...
    - Python's super() function in 2 Minutes: • Python's super() Funct...
    - How to Use *args and **kwargs in 2 Minutes: • How to Use *args and *...
    - Python's ABC (Abstract Base Class) in 2 Minutes: • Python's ABC (Abstract...
    - Python's _init_ Method in 2 Minutes: • Python's __init__ Meth...
    Python's _getitem_ Method in 2 Minutes: • Python's __getitem__ M...
    Subscribe to / @2minutespy for more such videos.
    @2MinutesPy
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @Hoxle-87
    @Hoxle-87 4 หลายเดือนก่อน +16

    This is better than gold! Clearly explained. No BS.

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Thanks!

  • @Darkev77
    @Darkev77 4 หลายเดือนก่อน +8

    Beautiful and elegant

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Thank you! Cheers!

  • @user-pm6qt6ee7p
    @user-pm6qt6ee7p 4 หลายเดือนก่อน +6

    Pure gold

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Thanks for appreciation.

  • @maxpythoneer
    @maxpythoneer 4 หลายเดือนก่อน +1

    I've worked with decorators and used them in my Python programs, there are several built-in decorators also that can ease your work. Great one...

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Good to know!

  • @sudhritiswaransinha9875
    @sudhritiswaransinha9875 หลายเดือนก่อน +2

    Please make a video on Concurrency:
    How do you achieve concurrency in Python? Please your explanation is so good !

    • @2MinutesPy
      @2MinutesPy  29 วันที่ผ่านมา

      definitely

  • @SunsetofMana
    @SunsetofMana 4 หลายเดือนก่อน +1

    Another amazingly simple, yet clear, and most impressively - concise explanation of a technical topic from this channel. Thank you and please keep making more!

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Glad you liked it!

  • @Rahul5556e
    @Rahul5556e 4 หลายเดือนก่อน

    I needed that one❤

  • @skbabasaba
    @skbabasaba 4 หลายเดือนก่อน +1

    Great job. Keep producing the great stuff.

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน +1

      Thanks, will do!

  • @abdallaalhag4425
    @abdallaalhag4425 4 หลายเดือนก่อน +1

    So much easier than the docs for beginners!!

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Great

  • @juanmanuelmillansanchez8165
    @juanmanuelmillansanchez8165 3 หลายเดือนก่อน +1

    I remember asking for this topic. Just wanted to thank you, got it perfectly!

    • @2MinutesPy
      @2MinutesPy  3 หลายเดือนก่อน +1

      got u bruh

  • @dweepverma3662
    @dweepverma3662 4 หลายเดือนก่อน +1

    Well explained... Keep it up

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Thanks a lot

  • @cyberlando
    @cyberlando 4 หลายเดือนก่อน +2

    I saw your response of someone asking why do you need the wrapper function. I still don’t understand why you couldn’t just place everything in the wrapper function just inside the scope of log_function(func) and return func()

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน +2

      What you're tryin to say is why can't we place the code directly within the log_function. Okay let me explain
      According to you:
      def log_function(func):
      print(f"Calling function {func.__name__}")
      return func()
      @log_function
      def say_hello():
      return "Hello, world!"
      This approach is okay but in limited cases, here log_function immediately calls the func() after printing the logging message. Now in this approach, you can't call the say_hello function as this will result in an error.
      print(say_hello()) # error
      print(say_hello) # code will execute
      There's no flexibility in this code, you can't do much.
      But but but... What if your function accepts some argument, how will you pass that argument?
      You can't pass it in the log_function, you need a nested function to handle all this. That's where a funtion like wrapper needed to preserve the signature of the original function and simultanoeusly modifying the behaviour. For instance, take a look at the following example:
      def log_function(func):
      def wrapper(*args):
      print(f"Calling function {func.__name__}")
      return func(*args)
      return wrapper
      @log_function
      def say_hello(text):
      return text
      print(say_hello("Hello"))
      Here, wrapper function handles all the stuff printing logging msg and returning the func with argument.
      Now when you called the say_hello function with an argument, it is passed to the log_function and log_function hand over to wrapper function to do the stuff. After all the the modification, the log_function returns the modified function.

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน +2

      I must say, that is a great question. Appreciate it.

    • @dweepverma3662
      @dweepverma3662 4 หลายเดือนก่อน

      The same question I thought in my mind, but got the answer here.

    • @dweepverma3662
      @dweepverma3662 4 หลายเดือนก่อน

      ​@@2MinutesPythanks for explaining this

  • @emilmemmedsoy770
    @emilmemmedsoy770 4 หลายเดือนก่อน

    Great explanation. Worth it more views

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน

      Glad you think so!

  • @user-eq4qk8qd2d
    @user-eq4qk8qd2d 4 หลายเดือนก่อน +1

    Why do I need a wrapper in my decorater function? What is the principle behind the wrapper?

    • @2MinutesPy
      @2MinutesPy  4 หลายเดือนก่อน +1

      The wrapper calls the original function and can modify the behaviour in some way which allows the decorator to extend the functionality of the original function without changing it's code directly.
      When you apply decorate a function, for instance,
      @log_function
      def say_hello():
      The above code is equivalent to say_hello = log_function(say_hello)
      Now this say_hello name points to the wrapper function within log_function and if you print (print(say_hello), you'll get the wrapper function reference, which means the wrapper function is needed to preserve the original function's signature while adding the extra behaviour or functionality.

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 3 หลายเดือนก่อน +1

    Well explained.

    • @2MinutesPy
      @2MinutesPy  3 หลายเดือนก่อน +1

      Thanks

  • @kwameadoko9542
    @kwameadoko9542 2 หลายเดือนก่อน

    Let’s say a better way of writing helpers.