zoukankan      html  css  js  c++  java
  • 定制自己的数据类型

    #继承
    """
    class LIST(list):
    def append(self, p_object):
    if not isinstance(p_object,int):
    raise TypeError('must be int')
    super().append(p_object)

    def insert(self, index, p_object):
    if not isinstance(p_object,int):
    raise TypeError('must be int')
    super().insert(index,p_object)

    li =LIST([1,2,3])
    print(li)
    #li.append('2')
    li.append(4)
    print(li)
    li.insert(0,-10)
    print(li)
    #li.insert(0,'-1')
    print(li)
    """
    #授权
    import time
    class Open:
    def __init__(self,filepath,m='w+',encode='utf-8'):
    self.filepath =filepath
    self.mode =m
    self.encoding =encode
    self.x =open(filepath,mode =m,encoding=encode)
    def write(self,line):
    t =time.strftime('%Y-%m-%d %X')
    self.x.write('%s %s'%(t,line))


    def __getattr__(self, item):
    print('%s %s'%(self,item))#getattr(self.x,item),中的item是字符串
    return getattr(self.x,item)#把它转化成self.x.item(属性),
    #print('%s %s'(self))
    f =Open('a')
    f.write('12112121 ')
    f.write('121212121 ')
    f.seek(0)
    print(f.read)#查询f.read,但是没有找到,f.read,所以就触发__getattr__方法
  • 相关阅读:
    「2019冬令营提高组」原样输出
    FJWC2019
    P2763 试题库问题
    P3979 遥远的国度
    P2754 [CTSC1999]家园
    P1251 餐巾计划问题
    P1382 楼房
    P1384 幸运数与排列
    P4294 [WC2008]游览计划
    P3345 [ZJOI2015]幻想乡战略游戏
  • 原文地址:https://www.cnblogs.com/IQ-Python/p/6758450.html
Copyright © 2011-2022 走看看