类的学习

#!/usr/bin/python

# -*- coding: utf-8 -*-


import re

import os

class Test()

    **************


class LogReader(Test): 这块表明LogReader继承了Test类的属性

    

    def __init__(self, logfile, bufferfile):

        self.logfile        = open(logfile, "r")

        self.bufferfile     = open(bufferfile, "r+")

        self.logfilesize    = os.path.getsize(logfile)

        self.error          = ""

        self.lastseek       = 0 


    def read(self):

        lastseek = re.findall("^\d+", self.bufferfile.read())

        if not lastseek or len(lastseek) != 1:

            lastseek = 0 

        else:

            lastseek = int(lastseek[0])

        if self.logfilesize < lastseek: 

            # log has roll back

            lastseek = 0 

        self.logfile.seek(lastseek)

        content = self.logfile.read()

        curseek = len(content) + lastseek

        self.bufferfile.seek(0)

        self.bufferfile.write(str(curseek))

        content_arr = [ row.strip() for row in content.split("\n") if row.strip() != "" ]

        return content_arr


    def __del__(self):

        self.logfile.close()

        self.bufferfile.close()


类的学习

 __init__初始化类的属性 __del__为类结束时消除的


如果想获取类的属性

a=LogReader() 实例化

a.logfile=b


self表示类的属性,可供别人去调用



别的脚本想调用这个类

import LogReader from logreader(文件名)


你可能感兴趣的:(python,import)