Skip to content

Commit f430f29

Browse files
committed
Decorator examples added
1 parent 5984c1b commit f430f29

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Concepts/decorator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##Illustrating the decorators concept
2+
# Example 1
3+
from datetime import datetime
4+
5+
def decorator_func(func):
6+
def wrapper(*args, **kwargs):
7+
if 7 <= datetime.now().hour < 16:
8+
func(*args, **kwargs)
9+
else:
10+
pass # Hush, the neighbors are asleep
11+
return wrapper

Concepts/decorator_call_1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
##Illustrating the decorator call concept
2+
# Example 1
3+
from decorator import decorator_func
4+
from datetime import datetime
5+
6+
def dec_call():
7+
print("This is implemented using the bald representation for decorators.")
8+
9+
def non_decorator_call():
10+
print("This is called without using a decorator.")
11+
12+
@decorator_func
13+
def dec_call_2():
14+
print("This is implemented using syntactic sugar representation for decorators.")
15+
16+
print("Time now = "+str(datetime.now().hour))
17+
dec_call = decorator_func(dec_call)
18+
19+
20+
dec_call()
21+
non_decorator_call()
22+
dec_call_2()

Concepts/decorator_call_2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
##Illustrating the decorator call with arguments concept
2+
# Example 1
3+
from decorator import decorator_func
4+
5+
@decorator_func
6+
def dec_call3(name):
7+
print(f"Hi this is the diff file call by {name}")
8+
9+
dec_call3("Abhiram")

0 commit comments

Comments
 (0)