Search This Blog

Saturday, July 5, 2014

Inheritance in Python

C:\Vim\vim72\Untitled.html A subclass in python is created like below

class <subclass name> (<super class name>):
    def __init__(self):
        #call the super class constructor
        <super class name>.__init__()

Example:
#example starts
#subclass of thread class is creted
class evThread(threading.Thread):
    #defining the constructor for the subclass
    def __init__(self, server, logtype):
        #super class constructor must be called. Otherwise, python will give a rruntime error: "RuntimeError("thread.__init__() not called")"
        threading.Thread.__init__(self)
        #define new attributes for subclass
        self.server = server
        self.logtype = logtype

    #overide the superclass run method
    def run(self):
        self.hand = win32evtlog.OpenEventLog(self.server,self.logtype)
        flags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
        total = win32evtlog.GetNumberOfEventLogRecords(self.hand)
        print 'Total Events: ', total

# Creating a thread object of subclass evThread
systemEventViewer = evThread('localhost', 'System')

#start the thread
systemEventViewer.start()

#example ends


Back to Python Home Page

No comments:

Post a Comment