-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdecorator.py
More file actions
37 lines (27 loc) · 718 Bytes
/
decorator.py
File metadata and controls
37 lines (27 loc) · 718 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
34
35
36
37
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# decorator.py -- Decorator usage of python.
# ----------------------------------------------------
# decorator without parameter.
def b(fn):
return lambda s: '<b>%s</b>' % fn(s)
def em(fn):
return lambda s: '<em>%s</em>' % fn(s)
@b
@em
def greet(name):
return 'Hello, %s!' % name
print(greet('world'))
# ----------------------------------------------------
# decorator with parameter.
def tag_wrap(tag):
def decorator(fn):
def inner(s):
return '<%s>%s</%s>' % (tag, fn(s), tag)
return inner
return decorator
@tag_wrap('b')
@tag_wrap('em')
def greet(name):
return 'Hello, %s!' % name
print(greet('world'))