Académique Documents
Professionnel Documents
Culture Documents
Functions
Lili Dworkin
University of Pennsylvania
... etc
def foo(**kwargs):
# kwargs is a dict of all keyword arguments
print ' '.join(['%s=%s' % (k,v) for (k, v) in
kwargs.items()])
>>> foo(a=1, b=2)
a=1 b=2
>>> foo(a=1, b=2, c=3)
a=1 c=3 b=2
>>> foo(**d)
vegetable=cauliflower
>>> a = [1,2,3]
>>> b = ['a','b','c']
>>> c = [4,5,6]
>>> zip(a,b)
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> zip(a,b,c)
[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6)]
>>> l = [a, b, c]
>>> zip(*l)
[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6)]
Scope
>>>
...
...
...
...
>>>
>>>
5
3
>>>
5
def foo(x):
print x
x = 3
print x
x = 5
foo(x)
Scope
>>>
...
...
...
...
>>>
>>>
[]
[0]
>>>
[0]
def foo(l):
print l
l.append(0)
print l
l = []
foo(l)
Scope
>>> x = 5
>>> def foo():
... y = x + 1
... x = 0
... return y
...
>>> foo()
UnboundLocalError: local variable 'x' referenced
before assignment
Scope
>>> x = 5
>>> def foo():
... y = x + 1 # refers to the x below
... x = 0
... return y
I
Scope
>>>
>>>
...
...
>>>
15
>>>
>>>
25
i = 10
def add(x):
return x + i
add(5)
i = 20
add(5)
Default Arguments
def foo(data=[]):
data.append(1)
return data
>>>
[1]
>>>
[1,
>>>
[1,
foo()
foo()
1]
foo()
1, 1]
Default Arguments
Default Arguments
>>>
>>>
...
...
>>>
>>>
10
i = 10
def foo(data=i): # data now points to 10
print data
i = 20
foo()
Default Arguments
Workaround:
def foo(data=None):
if data is None:
data = [] # local variable
# modify value here
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
We might be evaluating the same function over and over again ...
fibs = {}
def fib(n):
if n in fibs:
return fibs[n]
if n <= 1:
fibs[n] = n
else:
fibs[n] = fib(n-1) + fib(n-2)
return fibs[n]
Nested Functions
def custom_add(i):
def add(x):
return x + i
return add
>>>
>>>
6
>>>
>>>
11
add_five = custom_add(5)
add_five(1)
add_ten = custom_add(10)
add_ten(1)
Nested Functions
def custom_add(i):
def add(x):
return x + i
return add
>>>
>>>
>>>
15
>>>
>>>
15
i = 10
add_ten = custom_add(i)
add_ten(5)
i = 20
add_ten(5)
Nested Functions
functions = []
for i in range(10):
fun = custom_add(i)
functions.append(fun)
functions[0](1)
functions[1](1)
Nested Functions
A function like zip that always has list l as its first argument:
def custom_zip(l):
def my_zip(*args):
# remember that args is a tuple
zip_args = [l] + list(args)
# zip_args is a list of lists
return zip(*zip_args)
return my_zip
>>> z = custom_zip([1, 2, 3])
>>> z(['a', 'b', 'c'])
[(1, 'a'), (2, 'b'), (3, 'c')]
Nested Functions
def make_counter():
count = 0
def counter():
count = count + 1
return counter
>>> c = make_counter()
>>> c()
UnboundLocalError: local variable 'count' referenced
before assignment
Nested Functions
def make_counter():
count = 0
def counter():
count = count + 1
return counter
I
Nested Functions
def make_counter():
count = [0]
def counter():
count[0] = count[0] + 1
print count[0]
return counter
>>> c = make_counter()
>>> c()
1
>>> c()
2
Nested Functions
def make_counter():
count = [0]
def counter():
count[0] = count[0] + 1
print count[0]
return counter
I
>>>
[1,
>>>
...
...
>>>
[2,
Lambda Functions
expr(a1, a2 ...)
Lambda Functions
>>>
>>>
2
>>>
[1,
>>>
[0,
f = lambda x: x*2
f(1)
map(lambda x: x**2, [1, 2, 3])
4, 9]
filter(lambda x: x==0, [0, 1, 0, 2])
0]
Sorting
Sorting
Sorting
Sorting
Parsing
Goal:
>>> parse('2+3')
5
>>> parse('5-2')
3
def parse(s):
# assumes s is the form <int><op><int>
op = s[1]
if op == '+':
return int(s[0]) + int(s[2])
elif op == '-':
return int(s[0]) - int(s[2])
...
Parsing
>>>
>>>
>>>
def
Counting Characters