- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecorator.py
More file actions
Latest commit
63 lines (46 loc) · 1.01 KB
/
Copy pathDecorator.py
File metadata and controls
63 lines (46 loc) · 1.01 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# class Decorator(object):
# def __init__(self):
# self.func = func
# print "Created a decorator with: %s" % func
# @Decorator()
# def Foo(n):
# print "Foo: %d" % n
# return 2 * n
# Foo(2)
classdouble_it(object):
def__init__(self, f):
print"__init__()"
self.f=f
def__call__(self, *args):
print"__call__()"
return2*self.f(*args)
@double_it
defmultiply(x, y):
returnx*y
printmultiply(3, 4)
classcacher(object):
def__init__(self, func):
# print "__init__()"
self.cache= {}
self.func=func
def__call__(self, n):
# print "__call__(%d)" % n
ifninself.cache:
returnself.cache[n]
else:
result=self.func(n)
self.cache[n] =result
returnresult
NUM_CALCS=0
@cacher
deffibonnaci(n):
ifn==1orn==2:
return1
elifn<1:
returnNone
else:
globalNUM_CALCS
NUM_CALCS+=1
returnfibonnaci(n-1) +fibonnaci(n-2)
printfibonnaci(20)
print"# calcs: %d"%NUM_CALCS