recursive method in __init__

paul koelle paul at
Wed Nov 3 23:39:16 CET 2004

hi all,

I thought it would be nice to have instances that can "live" somehow in 
the "background" and do some tasks without being told so. The problem in 
the code below is: calling new_host = Host(ip, mac, collector) never 
"returns" because the the self.update() method gets called recursively. 
Im pretty sure I have to restructure the code and call the update() 
method from outside but I ask out of curiosity and to get some ideas how 
to do such things elegantly.


class Host(object):
     def __init__(self, ip, mac, collector):
         '''ip and mac are strings, collector is a Collector() class 
instance from the loganalyze module'''
         re_ip = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
         print "New host instance created"
         if not re_ip.match(ip):
             raise ValueError('Invalid IP address.')
             self._ip_addr = ip
         self._mac_addr = mac
         self.collector = collector
         # traffic counter
         self._minutes = []
         self._additional_ips = []

    def update(self):
         '''adds a dict with traffic for this host every minute'''
         print "Method update of class Host called"
         data = self.collector.get(self._ip_addr)
         if data:

More information about the Python-list mailing list