在Python中,变量可以指向函数
函数名也是变量
既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
def f(x): return x*x r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) print(list(r)) 运行结果: [1, 4, 9, 16, 25, 36, 49, 64, 81]
其效果类似:reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
from functools import reduce def add(x, y): return x + y r = reduce(add, [1, 3, 5, 7, 9]) print(r) 运行结果: 25
def is_odd(n): return n % 2 == 1 tmplist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) newlist = list(tmplist) print(newlist) 运行结果: [1, 3, 5, 7, 9]
print(sorted([36, 5, -12, 9, -21])) 运行结果:[-21, -12, 5, 9, 36] print(sorted([36, 5, -12, 9, -21], key=abs)) #abs(绝对值函数)此处也可使用 ~ 匿名函数 运行结果:[5, 9, -12, -21, 36]
高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回。
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum #将定义的函数sum()作为结果值返回 f = lazy_sum(1, 3, 5, 7, 9) f()
闭包是“返回函数”的一个典型应用
#闭包函数的实例 # outer是外部函数 a和b都是外函数的临时变量 def outer( a ): b = 10 def inner(): # inner是内函数 print(a+b) #在内函数中 用到了外函数的临时变量 return inner # 外函数的返回值是内函数的引用 if __name__ == ‘__main__‘: # 在这里我们调用外函数传入参数5 #此时外函数两个临时变量 a是5 b是10 ,并创建了内函数,然后把内函数的引用返回存给了demo # 外函数结束的时候发现内部函数将会用到自己的临时变量,这两个临时变量就不会释放,会绑定给这个内部函数 demo = outer(5) # 我们调用内部函数,看一看内部函数是不是能使用外部函数的临时变量 # demo存了外函数的返回值,也就是inner函数的引用,这里相当于执行inner函数 demo() # 15 demo2 = outer(7) demo2()#17
匿名函数的另一个别称是“lambda表达式”
lambda x: x*x 这个lambda表达式实际等同于: def f(x): return x*x >>> f = lambda x: x * x >>> f <function <lambda> at 0x101c6ef28> >>> f(5) 25
举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n, 用函数fact(n)表示
可以看出: fact(n) = n! = 1 x 2 x 3 x ... x (n-1) x n = (n-1)! x n = fact(n-1) x n
所以,fact(n)可以表示为n x fact(n-1),只有n=1时需要特殊处理。
于是,fact(n)用递归的方式写出来就是:
def fact(n): if n==1: return 1 return n * fact(n - 1)
如果我们计算fact(5),可以根据函数定义看到计算过程如下: ===> fact(5) ===> 5 * fact(4) ===> 5 * (4 * fact(3)) ===> 5 * (4 * (3 * fact(2))) ===> 5 * (4 * (3 * (2 * fact(1)))) ===> 5 * (4 * (3 * (2 * 1))) ===> 5 * (4 * (3 * 2)) ===> 5 * (4 * 6) ===> 5 * 24 ===> 120
递归函数的优点是定义简单,逻辑清晰。理论上,所有的递归函数都可以写成循环的方式,但循环的逻辑不如递归清晰。
缺点是过深的调用会导致栈溢出。
1) Exception: 所有异常类型 2) AttributeError: 特性引用或赋值失败时引发 3) IOError: 试图打开不存在的文件时引发 4) IndexError: 在使用序列中不存在的索引时引发 5) KeyError: 在使用映射时不存在的键时引发 6) NameError: 在找不到变量名字时引发 7) SyntaxError: 代码有语法错误时引发 8) TypeError: 函数应用于错误类型的对象时引发 9) ValueError: 函数应用于正确类型的对象,但该对象使用不合适的值时引发 10) ZeroDivisionError: 在除操作时第二个参数为0时引发 Python中各种内建异常
names = [‘alex‘,‘jack‘] try: names[2] except IndexError as e: print("列表操作错误",e) # 运行结果: 列表操作错误 list index out of range 捕获单个异常
try: x = input("Enter the first number:") y = input("Enter the second number:") print(x/y) except ZeroDivisionError: print("The second number can‘t zero") except NameError: print(‘That was not a number....‘) 多个except子句
说明:如果需要用一个块扑捉多个异常类型,那么可以将他们作为元组列出
try: x = input("Enter the first number:") y = input("Enter the second number:") print(x/y) except (ZeroDivisionError, TypeError, NameError): print("your numbers were bogus...") 一个except捕获多个异常
try: x = input("Enter the first number:") y = input("Enter the second number:") print(x/y) except (ZeroDivisionError, TypeError, NameError) as e: print(e) 捕捉对象: except (NameError) as e
import traceback try: name = int(‘df11‘) except Exception as e: print(traceback.format_exc()) # Traceback (most recent call last): # File "C:/Users/tom/Desktop/cmdb_cli_ser/AutoClient/test01.py", line 4, in <module> # name = int(‘df11‘) # ValueError: invalid literal for int() with base 10: ‘df11‘ traceback.format_exc()获取详细异常信息
try: x = input("Enter the first number:") y = input("Enter the second number:") print(x/y) except: print(‘something wrong happened‘) 正真的全捕捉: except
try: # 主代码块 pass except KeyError as e: # 异常时,执行该块 pass else: # 主代码块正常执行完,执行该块 pass finally: # 无论异常与否,最终执行该块 pass 异常使用结构
try: raise Exception(‘错误了。。。‘) except Exception as e: print(e) # 运行结果: 错误了。。。
class WupeiqiException(Exception): def __init__(self, msg): self.message = msg def __str__(self): return self.message #最终打印的结果就是这里return返回的值 try: raise WupeiqiException(‘我的异常‘) #这里的字符串就会传入到class类的msg中 except WupeiqiException as e: print(e) # 运行结果: 我的异常 自定义异常
作用:Python的assert是用来检查一个条件,如果它为真,就不做任何事。如果它为假,则会抛出AssertError并且包含错误信息
n = 1 assert type(n) is int print(‘aaaa‘) # 1. Assert后的断言结果成立时才会执行:print(‘aaaa‘) # 2. Assert后的断言结果不成立时会引发AssertError并退出程序
原文:https://www.cnblogs.com/8lala/p/12463007.html