calling destructor in python multiply-inherited class

Discussion in 'Python' started by Rajorshi, May 20, 2004.

  1. Rajorshi

    Rajorshi Guest

    Hi,
    I have two classes, say, Base1 and Base2. Now Derv is a class derived
    from both like
    class Derv(Base1,Base2).....
    In the destructor of the derived class, if I write something like this

    def __del__(self):
    for b in self.__class__.__bases__:
    b.__del__(self)

    then its working fine, but if I am doing something straightforward
    like

    def __del__(self):
    Base1.__del__(self)
    Base2.__del__(self)

    Its giving an error like :
    Exception exceptions.AttributeError: "'NoneType' object has no
    attribute '__del__'" in <bound method Derv.__del__ of <__main__.Derv
    instance at 0x8187484>> ignored

    Obviously I'm doing something wrong ! Any pointers ??

    Thanks in advance!
     
    Rajorshi, May 20, 2004
    #1
    1. Advertising

  2. Rajorshi

    Duncan Booth Guest

    (Rajorshi) wrote in
    news::

    > In the destructor of the derived class, if I write something like this
    >
    > def __del__(self):
    > for b in self.__class__.__bases__:
    > b.__del__(self)
    >
    > then its working fine, but if I am doing something straightforward
    > like
    >
    > def __del__(self):
    > Base1.__del__(self)
    > Base2.__del__(self)
    >
    > Its giving an error like :
    > Exception exceptions.AttributeError: "'NoneType' object has no
    > attribute '__del__'" in <bound method Derv.__del__ of <__main__.Derv
    > instance at 0x8187484>> ignored
    >
    > Obviously I'm doing something wrong ! Any pointers ??
    >


    I guess this error occurs when your program is exiting. When a Python
    program terminates the global variables in each module are set to None. The
    order in which this happens it undefined, so when your __del__ method gets
    called the global Base1, or maybe Base2, has already been cleared. The base
    classes still exist, so you can access them through your class, but the
    names they used will have been rebound.

    A reasonable conclusion from this is:
    A __del__ method must never access *any* global variables unless it is
    prepared to handle the consequences of the global not existing.

    Next question, do you really need to use __del__? Remember that Python will
    not always call the __del__ method at the time you expect, and that the
    behaviour will vary on different Python implementations. Also the existence
    of a __del__ method is sufficient to prevent your object ever being
    released from a cycle by the garbage collector. If you need explicit tidy
    up, use a dispose pattern instead of depending on __del__.

    There is very little that you can usefully do inside a __del__ method.
    Deleting contained objects is pointless (it happens automatically without
    you doing it in __del__). Closing a file should be done explicitly when you
    are finished with it because if you try to do it in __del__ and the call to
    __del__ is delayed you will get unexpected problems.

    If you really need to call __del__ on more than one base class, use the
    super() builtin to do it.
     
    Duncan Booth, May 20, 2004
    #2
    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. frs
    Replies:
    20
    Views:
    773
    Alf P. Steinbach
    Sep 21, 2005
  2. iTooo
    Replies:
    4
    Views:
    521
    Peter_Julian
    Nov 5, 2005
  3. wes
    Replies:
    1
    Views:
    314
  4. Janaka Perera
    Replies:
    12
    Views:
    497
    Janaka Perera
    May 30, 2007
  5. 7stud --
    Replies:
    11
    Views:
    426
    7stud --
    Nov 9, 2007
Loading...

Share This Page