-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecor1.py
More file actions
41 lines (30 loc) · 1.05 KB
/
decor1.py
File metadata and controls
41 lines (30 loc) · 1.05 KB
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
from collections import Counter
def count_characters(func):
# below is a nested, or inner function
def inner_function(mystr):
# some business logic
chars = Counter(mystr)
print(chars)
# here we invoke the function passed in as an argument
result = func(mystr)
# some more business logic
return result
# my_decorator_name() returns a reference to the inner function
return inner_function
# This is the general pattern I use for writing simple decorators
def my_decorator_name(func):
# below is a nested, or inner function
def inner_function(*args, **kwargs):
# some business logic
# here we invoke the function passed in as an argument
result = func(*args, **kwargs)
# some more business logic
return result
# my_decorator_name() returns a reference to the inner function
return inner_function
@count_characters
def printer(mystring):
print(mystring)
#myprint = count_characters(printer)
printer('hello world')
printer("Python")