眼镜店标志,前金融,屌丝男士第二季下载
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爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论