定义函数
函数是组织代码的基本单元。它把一段可复用的逻辑封装起来,通过名字调用,接受输入(参数),产生输出(返回值)。Python 用 def 关键字定义函数,语法简洁直观。
基本语法
def greet(name):
"""打印问候语。"""
print "Hello,", name
greet("Alice") # Hello, Alice
greet("Bob") # Hello, Bob
def 后面是函数名,圆括号内是参数列表,冒号结尾。缩进块是函数体。函数定义本身不会执行函数体,只有在被调用时才会执行。
返回值
函数用 return 语句返回值。没有 return 时,函数返回 None:
def add(a, b):
return a + b
result = add(3, 5)
print result # 8
def say_hello():
print "Hello"
result = say_hello()
print result # None
return 可以返回多个值,实际上是返回一个元组:
def get_stats(nums):
return min(nums), max(nums), sum(nums) / float(len(nums))
minimum, maximum, average = get_stats([1, 2, 3, 4, 5])
print minimum, maximum, average # 1 5 3.0
函数也是对象
在 Python 中,函数是一等公民(first-class citizen)。它可以被赋值给变量、作为参数传递、从函数返回:
def square(x):
return x ** 2
# 赋值给变量
my_func = square
print my_func(5) # 25
# 作为参数传递
def apply_func(f, x):
return f(x)
print apply_func(square, 5) # 25
# 从函数返回
def make_multiplier(n):
def multiplier(x):
return x * n
return multiplier
double = make_multiplier(2)
print double(5) # 10
triple = make_multiplier(3)
print triple(5) # 15
这种"函数作为值"的特性是 Python 支持函数式编程的基础。
空函数
函数体不能为空(会报 IndentationError),用 pass 占位:
def todo():
pass # 待实现
局部变量
函数内部赋值的变量是局部变量,只在函数内可见:
def calc():
x = 10 # 局部变量
return x * 2
print calc() # 20
print x # NameError: name 'x' is not defined
函数内部可以读取外部变量,但赋值会创建局部变量(除非用 global 声明):
count = 0
def increment():
count += 1 # UnboundLocalError!赋值让 count 变成局部变量
# 正确做法
def increment():
global count
count += 1
increment()
print count # 1
函数命名规范
- 函数名使用小写+下划线(snake_case):
calculate_average - 动词开头,描述动作:
get_data、process_item、is_valid - 返回布尔值的函数用
is_或has_前缀:is_empty、has_permission
递归函数
函数可以调用自身,这就是递归。递归必须有终止条件,否则会无限递归直到栈溢出:
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
print factorial(5) # 120
Python 默认递归深度限制为 1000:
import sys
print sys.getrecursionlimit() # 1000
# 可以修改(谨慎使用)
sys.setrecursionlimit(2000)
递归虽然优雅,但 Python 的函数调用开销较大,深度递归通常不如迭代高效。对于尾递归,Python 不会优化,所以 factorial(1000) 会耗尽栈空间。