order of object desctruction

Discussion in 'Python' started by Marcin Matuszkiewicz, Oct 28, 2003.

  1. 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
    Marcin Matuszkiewicz, Oct 28, 2003
    #1
    1. Advertising

  2. Marcin Matuszkiewicz

    bromden Guest

    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
    bromden, Oct 28, 2003
    #2
    1. Advertising

  3. Marcin Matuszkiewicz

    bromden Guest

    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
    bromden, Oct 28, 2003
    #3
  4. 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
    >
    Marcin Matuszkiewicz, Oct 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coltrane
    Replies:
    11
    Views:
    562
    Chris Uppal
    Feb 3, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    482
    Soren Kuula
    Feb 1, 2004
  3. Replies:
    12
    Views:
    701
    James Kanze
    Nov 27, 2007
  4. cspoh
    Replies:
    0
    Views:
    234
    cspoh
    Jul 31, 2003
  5. Stephan Kämper
    Replies:
    2
    Views:
    227
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page