当前位置:
首页 > 编程开发 > Python基础教程 >
-
python基础教程之python学习-69 包装和授权
本站最新发布 Python从入门到精通|Python基础教程
试听地址 https://www.xin3721.com/eschool/pythonxin3721/
试听地址 https://www.xin3721.com/eschool/pythonxin3721/
包装
1.二次加工标准类型(包装)
class List(list): def append(self, a_objcet): if type(a_objcet) is str: super().append(a_objcet) else: print('请传入字符转类型') l1 = List('hello') l1.append(123) l1.append('world') print(l1)
运行结果:
请传入字符转类型 ['h', 'e', 'l', 'l', 'o', 'world'] Process finished with exit code 0
2.授权
授权是包装的一个特性。授权的过程是所有更新的功能都是由新类的某部分来处理,但已存在的功能授权给对象的默认属性。
授权的关键就在于__getattr__方法。
# 创建一个open方法 class Open: def __init__(self,filname,mode='r',encoding='utf-8'): self.fil = open(filname,mode,encoding=encoding) self.mode = mode self.encoding = encoding def __getattr__(self, item): print('-------->',item,type(item)) return getattr(self.fil,item) f1 = Open('a.txt','w') print(f1.fil) print(f1.read) # 触发__getattr__
运行结果:
<_io.TextIOWrapper name='a.txt' mode='w' encoding='utf-8'> --------> read <class 'str'> <built-in method read of _io.TextIOWrapper object at 0x7f718a0007e0> Process finished with exit code 0
栏目列表
最新更新
python基础(15):内置函数(一)
python学习-69 包装和授权
python基础(16):内置函数(二)
画一个心送给心爱的小姐姐,Python绘图库
OS模块
Python win32com模块 合并文件夹内多个docx文
Python【day 14-5】sorted filter map函数应用和练
python中@property装饰器的使用
10、pytest -- skip和xfail标记
基本数据类型及内置方法(二)
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能
将Session值储存于SQL Server中
使用SQL Server数据库嵌套子查询的方法
SQL概述