Skip to content

Commit 7bf17e2

Browse files
Implemented decorator chaining program
1 parent aa9b6b9 commit 7bf17e2

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

add.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Calling add
2+
add returned

decorator_chaining_example1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ def wrapper(*args, **kwargs):
1717
def text_printer(text):
1818
print(text)
1919

20+
text_printer("This is my text")
21+
22+
@body
23+
@html
24+
def text_printer(text):
25+
print(text)
26+
2027
text_printer("This is my text")

logger.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def logger(filename="log.txt"):
2+
def logger_decorator(func):
3+
def wrapper(*args, **kwargs):
4+
with open(filename, "a") as logfile:
5+
logfile.write(f"Calling {func.__name__}\n")
6+
func(*args, **kwargs)
7+
logfile.write(f"{func.__name__} returned\n")
8+
return wrapper
9+
10+
return logger_decorator
11+
12+
13+
@logger(filename="add.log")
14+
def add():
15+
pass
16+
17+
18+
add()

0 commit comments

Comments
 (0)