Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/test/decorators.py
1396 views
1
# vim:fileencoding=utf-8
2
# License: BSD Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
3
4
def double(f):
5
return def (x):
6
return 2 * f(x)
7
8
def triple(f):
9
return def(x):
10
return 3 * f(x)
11
12
@double
13
def half(x):
14
return x // 2
15
16
@double
17
def f1(x):
18
return x
19
20
@double
21
@triple
22
def f2(x):
23
return x
24
25
def append_one(f):
26
return def(x):
27
ans = f(x)
28
ans.push(1)
29
return ans
30
31
def append_two(f):
32
return def(x):
33
ans = f(x)
34
ans.push(2)
35
return ans
36
37
@append_two
38
@append_one
39
def f3():
40
return []
41
42
o = {'double':double}
43
44
@o.double
45
def f4():
46
return 1
47
48
assrt.equal(2, half(2))
49
assrt.equal(4, f1(2))
50
assrt.equal(12, f2(2))
51
assrt.equal(2, f4())
52
assrt.deepEqual([1, 2], f3())
53
54
def multiply(amt):
55
56
def wrapper(f):
57
return def ():
58
return amt * f()
59
60
return wrapper
61
62
@multiply(2)
63
def two():
64
return 1
65
66
@multiply(3)
67
def three():
68
return 1
69
70
@multiply(2)
71
@multiply(2)
72
def four():
73
return 1
74
75
assrt.equal(2, two())
76
assrt.equal(3, three())
77
assrt.equal(4, four())
78
79