-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.py
More file actions
33 lines (27 loc) · 847 Bytes
/
Copy pathdecorators.py
File metadata and controls
33 lines (27 loc) · 847 Bytes
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
#JUST A NORMAL NESTED FUNCTION
def outer_function(msg):
def innter_function():
print(msg)
return innter_function
hifunc = outer_function("hi")
byefunc = outer_function("bye")
#hifunc()
#byefunc()
#DECORATOR FUNCTION
def decorator_function(original_function):
def wrapper_function(*args,**kwargs):
return original_function(*args,**kwargs)
return wrapper_function
def display():
print('display function ran')
decorated_display = decorator_function(display)
decorated_display()
#SAME AS DECORATOR FUNCTION
@decorator_function # same as display = decorator_function(display)
def display():
print("display function ran")
#INCLUDING A PARAMATERISED FUNCTION IN DECORATOR we need *args,**kwargs
@decorator_function
def display_info(name,age):
print('display_info ran with arguments ({},{})'.format(name,age))
display_info('john',25)