Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > order of object desctruction

Reply
Thread Tools

order of object desctruction

 
 
Marcin Matuszkiewicz
Guest
Posts: n/a
 
      10-28-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

 
Reply With Quote
 
 
 
 
bromden
Guest
Posts: n/a
 
      10-28-2003
please, read the warning here
http://www.python.org/doc/ref/customization.html

try declaring a global variable starting with an underscore (i
am not kidding) and see if it works then,

you can also try to secure yourself with a conditional:

def SendCommand(self, cmd):
if socket:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
...

--
bromden[at]gazeta.pl

 
Reply With Quote
 
 
 
 
bromden
Guest
Posts: n/a
 
      10-28-2003
this is really tricky,

if an imported module is still available within the __del__
method of some object seems to depend on implementation, version
and a bit of luck (then it's all about timing, i think),

so generally you should stick to the rule that "__del__()
methods should do the absolute minimum needed to maintain
external invariants" and use the conditional

--
bromden[at]gazeta.pl

 
Reply With Quote
 
Marcin Matuszkiewicz
Guest
Posts: n/a
 
      10-28-2003
I have moved a test of the xbert module to new file

import xbert

xb = xbert.xbert('xb')
print xb.Version()

And now there is no problem when calling __del__()

Marcin

bromden wrote:
> this is really tricky,
>
> if an imported module is still available within the __del__ method of
> some object seems to depend on implementation, version and a bit of luck
> (then it's all about timing, i think),
>
> so generally you should stick to the rule that "__del__() methods should
> do the absolute minimum needed to maintain external invariants" and use
> the conditional
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desctruction of objects inside a static nested map holding pointers jayaramganapathy@gmail.com C++ 12 11-27-2007 10:11 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
If you get an order # does it mean the order is accepted? =?Utf-8?B?U3RldmUxMDc3?= Windows 64bit 3 05-12-2005 11:46 PM
Traversion order cf. output order in XSL Soren Kuula XML 2 02-01-2004 09:10 AM
How to Display DropDownList with preserved order (custom order) cspoh ASP .Net Web Controls 0 07-31-2003 09:19 AM



Advertisments