[Revision] Decorator in Python
[Revision] Decorator in Python
in python
def func():
return 'I am a function.'
# Output
'I am a function.'
outer_function()
# Output
'I came from the inner function.'
Note that the inner_function is not available outside the outer_function. If we try to
execute the inner_function outside the outer_function, we receive a NameError exception.
inner_function()
# Output
Traceback (most recent call last):
File "/tmp/my_script.py", line 9, in <module>
inner_function()
NameError: name 'inner_function' is not defined
def outer_function():
'''Assign task to student'''
task = 'Programming'
def inner_function():
print(task)
return inner_function
homework = outer_function()
homework()
# Output
'Programming'
def friendly_reminder(func):
'''Reminder for husband'''
func()
print('Don\'t forget to bring your wallet!')
def action():
print('I am going to the store buy you something nice.')
# Output
'I am going to the store buy you something nice.'
"Don't forget to bring your wallet!"
def decorator_func(func):
def wrapper_func():
# Do something before the function.
func()
# Do something after the function.
return wrapper_func
@decorator_func
def original_func():
pass
return wrapper
@log_datetime
def daily_backup():
print('Daily backup job has finished.')
daily_backup()
# Output
'Function: daily_backup'
'Run on: 2023-08-19 20:45:11'
'Daily backup job has finished.'
return wrapper
@decorator
def original(a, b, c=None):
return a, b, c
original(1, 2, c=3)
1
#Output 2
c3
danial soleimany 12-15
Explanation of Code:
1. def decorator(func): This defines a decorator named decorator. It takes a single
argument, which is the function to be decorated.
2. Inside the decorator:
def wrapper(*args, **kwargs): This defines a wrapper function that will replace the
original function. The *args and **kwargs syntax allows the wrapper to accept any
number of positional and keyword arguments.
for i in args: This loop iterates through the positional arguments (args) passed to
the original function and prints each argument.
for k, v in kwargs.items(): This loop iterates through the keyword arguments
(kwargs) passed to the original function and prints both the keyword and its
associated value.
result = func(*args, **kwargs): This calls the original function (func) with the
provided arguments and keyword arguments, capturing its return value in the
result variable.
return result: The wrapper returns the result obtained from the execution of the
original function.
3. return wrapper: This line returns the wrapper function, which will replace the original
function when the decorator is applied.
4. @decorator: This is a decorator application. It means that the decorator is applied to
the following function, modifying its behavior.
5. def original(a, b, c=None): This defines a function named original that takes three
arguments: a, b, and an optional keyword argument c.
6. return a, b, c: This is the return statement of the original function. It returns a tuple
containing the values of the arguments a, b, and c.
7. original(1, 2, c=3): This line calls the original function with the arguments 1, 2, and c=3.
Due to the @decorator application, the original function is actually replaced with the
wrapper function defined within the decorator.
authentication system
for a web application