order of object desctruction

Marcin Matuszkiewicz marcin at finisar.com
Tue Oct 28 13:48:23 EST 2003


Code

---------------------------------------------------------------------------
import socket

class xbert:
     def __init__(self, name, host='127.0.0.1', port=1001):
         self.name = name
         self.host = host
         self.port = port

         self.SendCommand('%s New XgigBTS' % self.name)

     def __del__(self):
         print 'xbert destructor'
         self.SendCommand('%s Exit' % self.name)

     def SendCommand(self, cmd):
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         sock.connect((self.host, self.port))
         sock.send(cmd)
         ret = sock.recv(pow(2,15))
         sock.close()
         return ret

     def Version(self):
         return self.SendCommand('Frunner Ver')

if __name__ == '__main__':
     xb = xbert('xb')
     print xb.Version()
---------------------------------------------------------------------------
Result

---------------------------------------------------------------------------
0::1.7.20030127.1340

xbert destructor
Exception exceptions.AttributeError: "'NoneType' object has no attribute 
'socket'" in <bound method xbert.__del__ of <__main__.xbert instance at 
0x007A9EC8>> ignored
---------------------------------------------------------------------------

 From this example it seems that the instance of socket module is 
destructed before instance of the xbert class.  Is this interpretation 
correct?  This does not seem like a desired behavior.  Is there a way 
around it?

Thanks,

Marcin





More information about the Python-list mailing list