%普通函数定义
def add_numbers(x, y): return x + y add_numbers(1, 2)
输出:3
%函数缺省参数
def add_numbers(x,y,z=None): if (z==None): return x+y else: return x+y+z print(add_numbers(1, 2)) print(add_numbers(1, 2, 3))
输出:3
6
%添加标记 flag
def add_numbers(x, y, z=None, flag=False): if (flag): print('Flag is true!') if (z==None): return x + y else: return x + y + z print(add_numbers(1, 2, flag=True))
输出:
Flag is true! 3
%分配函数给变量a
def add_numbers(x,y): return x+y a = add_numbers a(1,2)
输出:
3