zoukankan      html  css  js  c++  java
  • python文件派生

    
    
    import time


    class Foo:
    x = 1

    def __init__(self, y):
    self.y = y

    def __getattr__(self, item): # 没有的情况下调用此方法
    print(item)

    def __setattr__(self, key, value):
    self.__dict__[key] = value

    def __delattr__(self, item):
    pass


    # f1 = Foo(1)
    # print(f1.y)
    #
    # print(dir(Foo.__dict__))


    # 文件包装 写入的时候加入时间
    class FileHandle:
    def __init__(self, filename, mode='r', encoding='utf8'):
    self.file = open(filename, mode, encoding=encoding)

    def write(self, line):
    t = time.strftime('%Y-%m-%d %X')
    self.file.write('{} {}'.format(t,line))

    def __getattr__(self, item):
    return getattr(self.file, item)


    f = FileHandle('a.txt', 'w+', 'utf8')
    f.write(1)
    f.read()
     
  • 相关阅读:
    Alpha发布——美工+文案展示博客
    021_func
    020_with
    018_IO
    017_set
    016_dict
    015_string
    012_list
    010_ternaryOperation
    008_standard_lib_os
  • 原文地址:https://www.cnblogs.com/majianyu/p/10210851.html
Copyright © 2011-2022 走看看