zoukankan      html  css  js  c++  java
  • Python的with用法理解

    Python的with理解

    标签(空格分隔): Python


    with做为一种上下文管理器,在Python中的作用可以简单的理解为是用来代替try...except...finally的处理流程。

    with通过__enter__方法初始化,然后在__exit__中做善后以及处理异常。对于一些需要预先设置,事后要清理的一些任务,with提供了一种非常方便的表达。在紧跟with后面的语句被求值运算后,会调用运算返回对象的__enter__方法,并将__enter__的返回结果赋值给as后面的变量。当with后面的代码块全部被执行完之后,将调用返回对象的__exit__()方法执行清理工作。

    就像在文件操作中:

    file = open("file.txt")
    try:
        data = file.read()
    finally:
        file.close()
    

    在使用了with...as...后,代码将精简为:

    with open("file.txt") as file:
        data = file.read()
    

    在上面的代码中,open()函数会返回一个类型为file的对象,该对象具有__enter____exit__方法(可以通过dir(file),type(file)查看),之后调用对象的__enter__方法,将返回值赋值给file变量,所以在使用with...as...操作文件时不需要显示的关闭文件。

    既然知道with...as...中最重要的就是__enter__exit__,那我们就可以来自定义一个上下文管理器。

    其中:
    __enter__方法将在进入代码块前被调用。
    __exit__将在离开代码块之后被调用(即使在代码块中遇到了异常)。
    class WithDemo:
        def __enter__(self):
            print "in __enter__"
            return "WithDemo"
            
        def __exit__(self, type, value, trace):
            print "in __exit__"
            
    def get_demo_with():
        return WithDemo()
        
    with get_demo_with() as demo:
        print "demo:", demo
    
    

    执行结果为:

    in __enter__
    demo:WithDemo
    in __exit__
    

    在刚开始的时候就已经说,with..as..可以用来代替try...except...finally...的流程操作,上面都在说try...finally...的情况,那异常呢?如果仔细点会发现__exit__()的参数有三个,这三个参数便是为异常提供处理的参数。

    class WithDemo:
        def __enter__(self):
            return self
            
        def __exit__(self, type, value, trace):
            print "type:", type
            print "value:", value
            print "trace:", trace
            
        def create_trace(self):
            return (1/10) + 10
            
    with WithDemo() as demo:
        demo.create_trace()
    

    跑一下代码就能发现代码打印出了type, value, trace。

  • 相关阅读:
    2020 7 13 每日随笔
    2020 7 10 每日总结
    2020 7 14 每日总结
    2020 7 16 每日总结
    2020 7 15 每日总结
    2020 7 19 每日总结
    2020 7 18 每日总结
    2020 7 17 每日总结
    2020 7 11
    2020 7 12
  • 原文地址:https://www.cnblogs.com/tingyugetc/p/5911148.html
Copyright © 2011-2022 走看看