filmov
tv
How Python Decorators Work

Показать описание
Can you explain how Python decorators work?
Python decorators are like wrappers. Python decorators alter the function of code before the code is executed or after it is executed.
I don't see the benefit.
It lets you either modify the function without modifying it directly, or it lets you augment the original function. Augmenting it to make it better is like decorating, which is why they call it a decorator.
Why would you need a decorator? Python functions are already objects that let you assign functions to variables, pass parameters to other functions, return functions and do calculations.
You can even define functions inside of other functions. But Python only allows read access to the outer scope of a function, not any assignment within a function.
That's called a closure, I believe.
Decorators let you modify the function without getting tripped up by closures.
So that's why you need a decorator -- to make the outside of the functional part pretty. How do you actually use a decorator?
You wrap a function with a decorator by pre-pending the function's definition with the decorator name and an at symbol.
I do not see why decorators are so important.
Python decorators as wrappers make the altered function very explicit.
Yes, I've heard Python wrappers and decorators are frustrating to the point of using a lot of explicit four letter words.
It simplifies the code to make it clear how you are altering the original function, improving the readability. Python is big on readability.
That's funny for a programming language named after a TV show.
You can write a decorator that alters the function of a function without changing any of the original functions.
That's not funny, just a tongue twister.
You can use decorator wrappers to trigger one function based on the results of another, to use the results to of one function to do something else and otherwise alter its results while maintaining your beautiful Python code.
Python decorators are like wrappers. Python decorators alter the function of code before the code is executed or after it is executed.
I don't see the benefit.
It lets you either modify the function without modifying it directly, or it lets you augment the original function. Augmenting it to make it better is like decorating, which is why they call it a decorator.
Why would you need a decorator? Python functions are already objects that let you assign functions to variables, pass parameters to other functions, return functions and do calculations.
You can even define functions inside of other functions. But Python only allows read access to the outer scope of a function, not any assignment within a function.
That's called a closure, I believe.
Decorators let you modify the function without getting tripped up by closures.
So that's why you need a decorator -- to make the outside of the functional part pretty. How do you actually use a decorator?
You wrap a function with a decorator by pre-pending the function's definition with the decorator name and an at symbol.
I do not see why decorators are so important.
Python decorators as wrappers make the altered function very explicit.
Yes, I've heard Python wrappers and decorators are frustrating to the point of using a lot of explicit four letter words.
It simplifies the code to make it clear how you are altering the original function, improving the readability. Python is big on readability.
That's funny for a programming language named after a TV show.
You can write a decorator that alters the function of a function without changing any of the original functions.
That's not funny, just a tongue twister.
You can use decorator wrappers to trigger one function based on the results of another, to use the results to of one function to do something else and otherwise alter its results while maintaining your beautiful Python code.