[Python] Function

Arguments

Something to be noticed when using default arguments.

1
2
3
def add_end(L=[]):
L.append('END')
return L
1
2
3
4
5
6
7
8
9
10
11
>>> add_end([1, 2, 3])
[1, 2, 3, 'END']
>>> add_end(['x', 'y', 'z'])
['x', 'y', 'z', 'END']
>>> add_end()
['END']

>>> add_end()
['END', 'END']
>>> add_end()
['END', 'END', 'END']

A new list is created once when the function is defined, and the same list is used in each successive call.

Python’s default arguments are evaluated once when the function is defined, not each time the function is called (like it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated that object for all future calls to the function as well.

The default arguments must be immutable objects!

1
2
3
4
5
def add_end(L=None):
if L == None:
L = []
L.append('END')
return L

Late Binding Closures

1
2
3
4
5
6
7
def create_multipliers():
return [lambda x : i * x for i in range(5)]

for multiplier in create_multipliers():
print(multiplier(2))

# RESULT: 8 8 8 8 8
1
2
3
4
5
6
7
8
9
10
from functools import partial
from operator import mul

def create_multipliers():
return [lambda x, i=i : i * x for i in range(5)]

def create_multipliers():
return [partial(mul, i) for i in range(5)]

# 0 2 4 6 8

Python’s closures are late binding. This means that the values of variables used in closures are looked up at the time the inner function is called.

Here, whenever any of the returned functions are called, the value of i is looked up in the surrounding scope at call time. By then, the loop has completed and i is left with its final value of 4.

Mutable Arguments

Mutable here means the number of arguments is mutable. It allows you to have 0 or many arguments.

1
2
3
4
5
6
7
8
def calc(*numbers): # * will turn numbers into a tuple
sum = 0
for n in numbers:
sum = sum + n * n
return sum

calc(1,2,3)
calc(1,2)

Keyword Arguments

Keyword argument allows you to have 0 or many arguments in the form of key-value pairs.

1
2
3
4
5
6
7
8
9
10
11
def person(name, age, **kw): 
print('name:', name, 'age:', age, 'other:', kw)

>>> person('Bob', 35, city='Beijing')
name: Bob age: 35 other: {'city': 'Beijing'}
>>> person('Adam', 45, gender='M', job='Engineer')
name: Adam age: 45 other: {'gender': 'M', 'job': 'Engineer'}

>>> extra = {'city': 'Beijing', 'job': 'Engineer'}
>>> person('Jack', 24, **extra) # kw will generate a copy of extra
name: Jack age: 24 other: {'city': 'Beijing', 'job': 'Engineer'}

Named keyword arguments

1
2
3
4
5
def person(name, age, *, city, job):
print(name, age, city, job)

>>> person('Jack', 24, city='Beijing', job='Engineer')
Jack 24 Beijing Engineer

Combinations

1
2
3
4
5
6
7
8
9
10
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)

def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)

>>> f1(1, 2)
a = 1 b = 2 c = 0 args = () kw = {}
>>> f1(1, 2, 3, 'a', 'b', x=99)
a = 1 b = 2 c = 3 args = ('a', 'b') kw = {'x': 99}

Recursion

General recursion will cause stack overflow.

1
2
3
4
def fact(n):
if n==1:
return 1
return n * fact(n - 1)

使用递归函数需要注意防止栈溢出。在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出。

解决递归调用栈溢出的方法是通过尾递归优化,事实上尾递归和循环的效果是一样的,所以,把循环看成是一种特殊的尾递归函数也是可以的。

尾递归是指,在函数返回的时候,调用自身本身,并且,return语句不能包含表达式。这样,编译器或者解释器就可以把尾递归做优化,使递归本身无论调用多少次,都只占用一个栈帧,不会出现栈溢出的情况。

1
2
3
4
def fact_iter(num, product):
if num == 1:
return product
return fact_iter(num - 1, num * product)

尾递归调用时,如果做了优化,栈不会增长,因此,无论多少次调用也不会导致栈溢出。

遗憾的是,大多数编程语言没有针对尾递归做优化,Python解释器也没有做优化,所以,即使把上面的fact(n)函数改成尾递归方式,也会导致栈溢出