forked from zhongyingqun/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_test.py
More file actions
44 lines (38 loc) · 862 Bytes
/
decorator_test.py
File metadata and controls
44 lines (38 loc) · 862 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
38
39
40
41
42
43
44
def decorator(F):
def new_F(a,b):
print "input:"
print a,b
return F(a,b)
return new_F
@decorator
def square_sum(a,b):
return a**2 + b**2
print(square_sum(3,4))
"""
def decorator(aClass):
class newClass:
def __init__(self, age):
self.total_display = 0
self.wrapped = aClass(age)
def display(self):
self.total_display += 1
print("total display", self.total_display)
self.wrapped.display()
return newClass
@decorator
class Bird:
def __init__(self, age):
self.age = age
def display(self):
print("My age is",self.age)
eagleLord = Bird(5)
for i in range(3):
eagleLord.display()
output:
('total display', 1)
('My age is', 5)
('total display', 2)
('My age is', 5)
('total display', 3)
('My age is', 5)
"""