Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python decorating functions before call

I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible?

like image 552
Vasil Avatar asked Nov 11 '08 22:11

Vasil


People also ask

How do you call a decorator function in Python?

The decorators can be used to inject modified code in functions or classes. The decorators allow the program to be modified to add any code that has a special role to play. Decorators are called before the definition of the function you have decorated. def decorating(function):

How do you decorate a print in Python?

Decorators in Python are the design pattern that allows the users to add new functionalities to an existing object without the need to modify its structure. Decorators are generally called before defining a function the user wants to decorate. # inside the wrapper function. def function_to_be_used():

How do you decorate a Python method?

To decorate a method in a class, first use the '@' symbol followed by the name of the decorator function. A decorator is simply a function that takes a function as an argument and returns yet another function.

Why you should wrap decorators in Python?

A Python decorator wraps a target function with another wrapper function. This wrapper function can add any behavior you might want. For example, it can track execution times, redefine how the wrapped function runs, or modify return values. As a concrete example, the standard library's functools.


1 Answers

With:

decorator(original_function)()

Without:

original_function()

A decorator is just a function which takes a function as an argument and returns another one. The @ syntax is totally optional. Perhaps a sift through some documentation might help clarify things.

like image 175
Ali Afshar Avatar answered Sep 23 '22 15:09

Ali Afshar