Python之面向对象上下文管理协议
Python之面向对象上下文管理协议
析构函数:
import time
class Open:
def __init__(self,filepath,mode='r',encode='utf-8'):
self.f=open(filepath,mode=mode,encoding=encode) def write(self):
pass def __getattr__(self, item):
return getattr(self.f,item) def __del__(self):
print('----->del')
self.f.close() f=Open('a.txt','w')
f1=f
del f print('=========>')
上下文管理协议:
# with open('a.txt','r') as f:
# print('--=---->')
# print(f.read()) # with open('a.txt', 'r'):
# print('--=---->') #
class Foo:
def __enter__(self):
print('=======================》enter')
return 111111111111111 def __exit__(self, exc_type, exc_val, exc_tb):
print('exit')
print('exc_type',exc_type)
print('exc_val',exc_val)
print('exc_tb',exc_tb)
return True # with Foo(): #res=Foo().__enter__()
# pass with Foo() as obj: #res=Foo().__enter__() #obj=res
print('with foo的自代码块',obj)
raise NameError('名字没有定义')
print('************************************') print('')
# import time
# class Open:
# def __init__(self,filepath,mode='r',encode='utf-8'):
# self.f=open(filepath,mode=mode,encoding=encode) # def write(self,line):
# self.f.write(line) # def __getattr__(self, item):
# return getattr(self.f,item) # def __del__(self):
# print('----->del')
# self.f.close() # def __enter__(self):
# return self.f
# def __exit__(self, exc_type, exc_val, exc_tb):
# self.f.close() # with Open('George'.txt','w') as f:
# f.write('Georgetest\n')
# f.write('Georgetest\n')
# f.write('Georgetest\n')
# f.write('Georgetest\n')
# f.write('Georgetest\n') class Open:
def __init__(self,filepath,mode,encode='utf-8'):
self.f=open(filepath,mode=mode,encoding=encode)
self.filepath=filepath
self.mode=mode
self.encoding=encode def write(self,line):
print('write')
self.f.write(line) def __getattr__(self, item):
return getattr(self.f,item) def __enter__(self):
return self def __exit__(self, exc_type, exc_val, exc_tb):
self.f.close()
return True with Open('aaaaa.txt','w') as write_file: #write_file=Open('aaaaa.txt','w')
write_file.write('123123123123123\n')
write_file.write('123123123123123\n')
print(sssssssssssssss)
write_file.write('123123123123123\n')
Python之面向对象上下文管理协议的更多相关文章
- python 描述符 上下文管理协议 类装饰器 property metaclass
1.描述符 #!/usr/bin/python env # coding=utf-8 # 数据描述符__get__ __set__ __delete__ ''' 描述符总结 描述符是可以实现大部分py ...
- python基础----实现上下文管理协议__enter__和__exit__
我们知道在操作文件对象的时候可以这么写 with open('a.txt') as f: '代码块' 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明_ ...
- Python系列之 - 上下文管理协议
with obj as f: '代码块' 1.with obj ---->触发obj.__enter__(),拿到返回值 2.as f----->f=返回值. 3.with obj as ...
- python 面向对象编程 之 上下文管理协议
with open('path', 'r' ,encoding='utf-8') as f: 代码块 上述就叫做上线文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明 ...
- Python上下文管理协议:__enter__和__exit__
上下文管理器(context manager)是Python2.5开始支持的一种语法,用于规定某个对象的使用范围.一旦进入或者离开该使用范围,会有特殊操作被调用 (比如为对象分配或者释放内存).它的语 ...
- Python 上下文管理协议中的__enter__和__exit__基本理解
所谓上下文管理协议,就是咱们打开文件时常用的一种方法:with __enter__(self):当with开始运行的时候触发此方法的运行 __exit__(self, exc_type, exc_va ...
- 用python优雅打开文件及上下文管理协议
有次面试被问到如何优雅地打开一个文件? 那就是用with语句,调用过后可以自动关闭. 但是为什么使用with语句就可以自动关闭呢,原因就是上下文管理协议. 上下文管理协议:包含方法 __e ...
- python - 上下文管理协议(with + __enter__ + __exit__)
上下文管理协议: with + __enter__ + __exit__ #上下问管理协议: #with + __enter__ + __exit__ class Test(): def __init ...
- Python概念-上下文管理协议中的__enter__和__exit__
所谓上下文管理协议,就是咱们打开文件时常用的一种方法:with __enter__(self):当with开始运行的时候触发此方法的运行 __exit__(self, exc_type, exc_va ...
随机推荐
- bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】
要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...
- C++中的static修饰的变量和函数
原文地址:http://blog.csdn.net/he3913/archive/2008/09/18/2944737.aspxC++里的静态成员函数(不能用const的原因+static在c++中的 ...
- [ZPG TEST 105] 扑克游戏【Huffman】
扑克游戏 (poker) 题目描述: 有一棵无穷大的满二叉树,根为star,其余所有点的权值为点到根的距离,如图: 现在你有一些扑克牌,点数从1到13,你要把这些扑克牌全部放到这个树上: 当你把点数为 ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- 配置Ubuntu16.04第01步:U盘安装 Ubuntu 16.04系统
Ubuntu 每年发布两个版本,Ubuntu 16.04 开发代号为“Xenial Xerus”,为第六个长期支持(LTS)版本. 1. 制作U盘系统安装盘 1.1下载最新的Universal USB ...
- ACM_回文素数
回文素数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 131号是一个主回文,因为它是一个素数和一个回文(当向后读时,它是相同的 ...
- Linux环境下源码安装Apache2.2.25
操作环境:RedHat Enterprise Linux 5.6 一.安装准备 安装Apache一般依赖3个组件:apr.apr-util.pcre. 确保这三个组件已经安装. [root@bigsr ...
- 如何用C#动态编译、执行代码[转]
原文链接 在开始之前,先熟悉几个类及部分属性.方法:CSharpCodeProvider.ICodeCompiler.CompilerParameters.CompilerResults.Assemb ...
- 12c debug 转 12C 连接CDB和PDB
来源:David Dai -- Focus on Oracle 连接到CDB 12c debug 和普通实例一样的连接. 指定ORACLE_SID 以后可以使用OS认证,也可以使用密码进行连接. [o ...
- rhel7安装oracle 11gR2,所需的依赖包
binutils-2.23.52.0.1-30.el7.x86_64 compat-libstdc++-33-3.2.3-61.x86_64compat-libstdc++-33-3.2.3-61.i ...