1. python函数 不同于其他语言,python支持函数返回多个值 为函数提供说明文档:help(函数名)或者函数名.__doc__ def str_max(str1, str2): ''' 比较两个字符串的大小 ''' str = str1 if str1 > str2 else str2 return str help(str_max) print(str_max.__doc__) Help on built-in function len in module builtins: len
"""lambda 参数列表 : 返回值lambda 参数形式: 1.无参数:lambda:100 2.一个参数:lambda a: a 3.默认参数:lambda a, b, c=100: a + b + c 4.可变参数:*args,返回元组 lambda *args: args 5.可变参数:*kwargs,返回字典 lambda **kwargs: kwargs lambda 应用:简化代码 1.判断,lambda a, b: a if a > b else b
# if we need it only once and it's quite simple def make_incrementor(n): return lambda x: x + n f = make_incrementor(2) print f(3) # filter, map, reduce foo = [2, 18, 9, 22, 17, 24, 8, 12, 27] print filter(lambda x: x % 3 == 0, foo) print map(lambda