1. 空函数
如果想定义一个什么事也不做的空函数,可以用pass语句:
def nop():
pass
2. 参数检查
python 只会帮我们检查参数个数,而不会帮我们检查参数类型
3. 返回值
python 函数允许返回多个值,并赋给多个变量
import math
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
>>> x, y = move(100, 100, 60, math.pi / 6)
>>> print x, y
151.961524227 70.0
这里实际上函数还是返回了一个 tuple 类型结果
>>> r = move(100, 100, 60, math.pi / 6)
>>> print r
(151.96152422706632, 70.0)