-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_scrap.py
59 lines (44 loc) · 1000 Bytes
/
functions_scrap.py
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
def momentum(mass, vel):
""" Computes the momentum of a particle. """
mass += 1
return mass * vel
mass = 10.0
print momentum(mass, 0.8)
print mass
def append_one(l):
l.append(1)
# kwargs stands for keyword args
def draw(x, y, *args, **kwargs):
print 'Draw', x, y
print 'Args', args
print 'Kwargs', kwargs
draw(1,2, 3, 4, color='red', height=43)
def add(x, y):
return x + y
def sub(x, y):
return x - y
def print_then_call(f, *args):
print 'Args', args
return f(*args)
def logging(f):
def wrapped_f(*args):
print 'Args', args
return f(*args)
return wrapped_f
@logging
def mult(x, y):
return x * y
def cached(f):
cache = {}
def cached_f(*args):
if args in cache:
return cache[args]
result = f(*args)
cache[args] = result
return result
return cached_f
import time
@cached
def div(x, y):
time.sleep(1.5)
return x/y