Python 奇技淫巧
显示有限的接口到外部
<a href="http://click.aliyun.com/m/24149/">#!/usr/bin/env python</a>
<a href="http://click.aliyun.com/m/24149/"># -*- coding: utf-8 -*-</a>
<a href="http://click.aliyun.com/m/24149/">from base import APIBase</a>
<a href="http://click.aliyun.com/m/24149/">from client import Client</a>
<a href="http://click.aliyun.com/m/24149/">from decorator import interface, export, stream</a>
<a href="http://click.aliyun.com/m/24149/">from server import Server</a>
<a href="http://click.aliyun.com/m/24149/">from storage import Storage</a>
<a href="http://click.aliyun.com/m/24149/">from util import (LogFormatter, disable_logging_to_stderr,</a>
<a href="http://click.aliyun.com/m/24149/">enable_logging_to_kids, info)</a>
<a href="http://click.aliyun.com/m/24149/">__all__ = ['APIBase', 'Client', 'LogFormatter', 'Server',</a>
<a href="http://click.aliyun.com/m/24149/">'Storage', 'disable_logging_to_stderr', 'enable_logging_to_kids',</a>
<a href="http://click.aliyun.com/m/24149/">'export', 'info', 'interface', 'stream']</a>
with的魔力
with语句需要支持上下文管理协议的对象, 上下文管理协议包含__enter__
和__exit__
两个方法。 with语句建立运行时上下文需要通过这两个方法执行进入和退出操作。
其中上下文表达式是跟在with之后的表达式, 该表达式返回一个上下文管理对象。
<a href="http://click.aliyun.com/m/24149/"># 常见with使用场景</a>
<a href="http://click.aliyun.com/m/24149/">with open("test.txt", "r") as my_file: # 注意, 是__enter__()方法的返回值赋值给了my_file,</a>
<a href="http://click.aliyun.com/m/24149/">for line in my_file:</a>
<a href="http://click.aliyun.com/m/24149/">print line</a>
详细原理可以查看这篇文章, 浅谈 Python 的 with 语句。
知道具体原理,我们可以自定义支持上下文管理协议的类,类中实现__enter__
和__exit__
方法。
<a href="http://click.aliyun.com/m/24149/">#!/usr/bin/env python</a>
<a href="http://click.aliyun.com/m/24149/"># -*- coding: utf-8 -*-</a>
<a href="http://click.aliyun.com/m/24149/">class MyWith(object):</a>
<a href="http://click.aliyun.com/m/24149/">def __init__(self):</a>
<a href="http://click.aliyun.com/m/24149/">print "__init__ method"</a>
<a href="http://click.aliyun.com/m/24149/">def __enter__(self):</a>
<a href="http://click.aliyun.com/m/24149/">print "__enter__ method"</a>
<a href="http://click.aliyun.com/m/24149/">return self # 返回对象给as后的变量</a>
<a href="http://click.aliyun.com/m/24149/">def __exit__(self, exc_type, exc_value, exc_traceback):</a>
<a href="http://click.aliyun.com/m/24149/">print "__exit__ method"</a>
<a href="http://click.aliyun.com/m/24149/">if exc_traceback is None:</a>
<a href="http://click.aliyun.com/m/24149/">print "Exited without Exception"</a>
<a href="http://click.aliyun.com/m/24149/">return True</a>
<a href="http://click.aliyun.com/m/24149/">else:</a>
<a href="http://click.aliyun.com/m/24149/">print "Exited with Exception"</a>
<a href="http://click.aliyun.com/m/24149/">return False</a>
<a href="http://click.aliyun.com/m/24149/">def test_with():</a>
<a href="http://click.aliyun.com/m/24149/">with MyWith() as my_with:</a>
<a href="http://click.aliyun.com/m/24149/">print "running my_with"</a>
<a href="http://click.aliyun.com/m/24149/">print "------分割线-----"</a>
<a href="http://click.aliyun.com/m/24149/">with MyWith() as my_with:</a>
<a href="http://click.aliyun.com/m/24149/">print "running before Exception"</a>
<a href="http://click.aliyun.com/m/24149/">raise Exception</a>
<a href="http://click.aliyun.com/m/24149/">print "running after Exception"</a>
<a href="http://click.aliyun.com/m/24149/">if __name__ == '__main__':</a>
<a href="http://click.aliyun.com/m/24149/">test_with()</a>
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16