包装
# 包装(二次加工标准类型)
# 继承 + 派生 的方式实现 定制功能
# 示例:
#
class list_customization(list):
#重新定制append方法,判断添加的数据类型是否是str类型.
def append(self, object):
# print(type(object))
if type(object) is str:
super().append(object)
else:
print("添加类型错误...")
a = "XXXXXXXXXXXX"
b = list_customization(a)
c = 2
b.append(c)
print(b)
授权(其实也是一种包装)
#授权
#授权也是一种包装
#授权的过程,即是所有更新的功能,都是由新类的某部分来处理
#已存在的功能就授权给对象的默认属性
# 示例:
class Open_authorization():
def __init__(self,file_path,mode):
self.file = open(file_path,mode)
def __getattr__(self, item):
#利用getattr()来调用对象处理
return getattr(self.file,item)
a = Open_authorization("info.log",'r')
print(a.read())