-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator_examples.py
60 lines (39 loc) · 1.25 KB
/
decorator_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 10 22:53:11 2021
@author: mjach
"""
'''
Decorator
- in python a decorator is a function and receive a function as a argument.
- it helps to add extra functionalities without modifying actual function.
- in python function can be passed as a argument - you have seen map , reduce
- we can create function within another function
- function can return another function
- syntax of the decorator is start with @ sign
'''
# def python_basic():
# print('So far we have learned python basic.')
# def python_advance(function):
# #function()
# print('Whats up..')
# function()
#python_advance(python_basic)
def python_advance(function): # outer function/decorator function
def python_object_oriented(): # inner funciton
function()
print('Now, we are larning python advance topics..')
#function() # calling outer function/decorator function
return python_object_oriented # return inner function
@python_advance
def python_basic():
print('So far we have learned python basic.')
python_basic()
def calculator(function):
def smart_calculator(a,b):
return function(a,b)
return smart_calculator
@calculator
def addition(a,b):
print(a * b)
addition(5, 5)