-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmore_decoration2.py
More file actions
40 lines (27 loc) · 1.02 KB
/
Copy pathmore_decoration2.py
File metadata and controls
40 lines (27 loc) · 1.02 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
def hello(fn):
def wrapper():
print("hello, %s" % fn.__name__)
fn()
print("goodby, %s" % fn.__name__)
return wrapper
# @hello
# def foo(): #foo = hello(foo)
# print("i am foo")
def foo():
print("i am foo")
foo = hello(foo)#hello(foo)返回了wrapper()函数,所以,foo其实变成了wrapper的一个变量
foo() #foo()执行其实变成了wrapper()
#带参数的decrorator
def makeHtmlTag(tag, *args, **kwds):
def real_decorator(fn):
css_class = " class='{0}'".format(kwds["css_class"]) if "css_class" in kwds else ""
def wrapped(*args, **kwds):
return "<" + tag + css_class + ">" + fn(*args, **kwds) + "</" + tag + ">"
return wrapped
return real_decorator
@makeHtmlTag(tag="b", css_class="bold_css")
@makeHtmlTag(tag="i", css_class="italic_css")
@makeHtmlTag(tag="c", css_class="usa_css")
def hello(): #hello = makeHtmlTag(tag="b", css_class="bold_css")((makeHtmlTag(tag="i", css_class="italic_css")(hello))
return "style"
print(hello())