1

Python with语句 | CHEGVA

 1 year ago
source link: https://chegva.com/5473.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python进阶(28)—with语句

◎知识点

  1. with语句使用及异常处理

  2. with语句执行流程

◎脚本练习

"""with语句"""

"""
    如果一个类对象实现了特殊方法__enter__()和__exit__(),那么就称这个类对象遵守了上下文管理协议,
同时,这个类对象的实例对象被称为上下文管理器。
    with语句会让上下文管理器创建一个运行时上下文,当进入运行时上下文时自动调用特殊方法__enter__(),
当离开运行上下文时自动调用特殊方法__exit__()。
    with语句的语法格式:
    with 上下文表达式 [as 变量]:
        with语句体
        
    如果with语句体中产生了异常,那么sys.exc_info()的返回值中的三个元素会被自动传递给特殊方法
__exit__()的形参exc_type, exc_val, exc_tb, 这三个形参分别表示异常的类型、异常的错误信息和
异常调用堆栈的跟踪信息

    与finally从句类似,特殊方法__exit__()总会被调用,通常在特殊方法__exit__()中释放资源,
例如:关闭文件、关闭网络连接等
"""

class MyContextManager(object):
    def __enter__(self):
        print("特殊方法__enter__()被调用")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("特殊方法__exit__()被调用")

        print("异常的类型:%s" % exc_type)
        print("异常的错误信息:%s" % exc_val)
        print("异常调用堆栈的跟踪信息:%s" % exc_tb)

        # return True

    def do_sth(self):
        print("方法do_sth()被调用")
        print(1 / 0)

"""
with MyContextManager() as mcm:
    mcm.do_sth()
"""

"""
    当with语句体中产生异常,并且特殊方法__exit__()没有返回True时,为了让程序能够继续执行,
可以使用try-except语句对抛出的异常实例对象进行捕获和处理。
"""

try:
    with MyContextManager() as mcm:
        mcm.do_sth()
except ZeroDivisionError as err:
    print(err)
Python

Python进阶(28)—with语句

◎脚本地址:https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_advanced/with.py

安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5473.html | ☆★★每天进步一点点,加油!★★☆ | 

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK