Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorators on functions

People also ask

Why do we use decorators with functions?

Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated. Using decorators in Python also ensures that your code is DRY(Don't Repeat Yourself).

What do decorators do in Python?

A decorator in Python is a function that takes another function as its argument, and returns yet another function . Decorators can be extremely useful as they allow the extension of an existing function, without any modification to the original function source code.

What is a decorator in programming?

In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other objects from the same class.

Can we use decorator inside a function in Python?

Nesting means placing or storing inside the other. Therefore, Nested Decorators means applying more than one decorator inside a function. Python allows us to implement more than one decorator to a function. It makes decorators useful for reusable building blocks as it accumulates the several effects together.


To execute a decorator, you evaluate an expression and doing that prevents hoisting (even for a variable declaration, the right-hand side of an assignment stays put). Therefore, it is not compatible with function declarations being hoisted.

As a work-around, I suggested that function expressions, generator function expressions and arrow functions could be enabled to be decorated:

const func = @someDecorator('abc') (x, y) => { return x + y };

Alas, that wasn’t met with much enthusiasm: Decorators for functions


You certainly have a point here.

But as neo_blackcap pointed out, function decorator are not part of the ES7 decorators draft.

So, the best thing you can do is to start discussion on the corresponding tracker to attract community attention to your proposal.

ES7 decorators are on their first stage of development second stage of development, meaning that their API is still under development and could undergo any change.


I think the problem is function decorator has not been ES7 draft.

Of course, you still can implement your function decorator by yourself