oop - Inheritance in Python, init method overrriding -


i'm trying understand inheritance in python. have 4 different kind of logs want process: cpu, ram, net , disk usage

i decided implement classes, they're formally same except log file reading , data type data. have following code (log object logging object instance of custom logging class)

class logfile():         def __init__(self,log_file):         self._log_file=log_file         self.validate_log()      def validate_log(self):         try:             open(self._log_file) dummy_log_file:                 pass         except ioerror e:                log.log_error(str(e[0])+' '+e[1]+' log file '+self._log_file)  class data(logfile):     def __init__(self,log_file):         logfile.__init__(self, log_file)         self._data=''      def get_data(self):         return self._data      def set_data(self,data):         self._data=data      def validate_data(self):         if self._data == '':             log.log_debug("empty data list")  class datacpu(data):         def read_log(self):                     self.validate_log()         reading , writing list stuff          return list   class dataram(data):     def read_log(self):                     self.validate_log()             reading , writing list stuff          return list  class datanet(data): 

now want datanet class object of data class more attributes, in particular dictionary every 1 of interfaces. how can override __init__() method same data.__init__() adding self.dict={} without copying data builder? is, without explicitly specifing datanet objects have ._data attribute, inherited data.

just call data.__init__() method datanet.__init__(), set self._data = {}:

class datanet(data):     def __init__(self, logfile):         data.__init__(self, logfile)         self._data = {} 

now whatever data.__init__() self happens first, leaving datanet initializer add new attributes or override attributes set parent initializer.

in python 3 classes new-style, if python 2, i'd add object base class logfile() make new-style too:

class logfile(object): 

after can use super() automatically parent __init__ method call; has advantage in more complex cooperative inheritance scheme right methods invoked in right order:

class data(logfile):     def __init__(self,log_file):         super(data, self).__init__(log_file)         self._data = ''  class datanet(data):     def __init__(self, logfile):         super(datanet, self).__init__(logfile)         self._data = {} 

super() provides bound methods, don't need pass in self argument __init__ in case. in python 3, can omit arguments super() altogether:

class data(logfile):     def __init__(self,log_file):         super().__init__(log_file)         self._data = ''  class datanet(data):     def __init__(self, logfile):         super().__init__(logfile)         self._data = {} 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -