Re: super not working in __del__ ?

Discussion in 'Python' started by Fredrik Lundh, Feb 16, 2005.

  1. Christopher J. Bottaro wrote:

    >I get this exception when I run the following code:
    >
    > Exception exceptions.TypeError: 'super() argument 1 must be type, not None'
    > in <bound method Txrposdn.__del__ of <__main__.Txrposdn object at
    > 0xf6f7118c>> ignored


    reading the documentation never hurts:

    http://docs.python.org/ref/customization.html

    "Warning: Due to the precarious circumstances under which __del__()
    methods are invoked, exceptions that occur during their execution are
    ignored, and a warning is printed to sys.stderr instead. Also, when
    __del__() is invoked in response to a module being deleted (e.g.,
    when execution of the program is done), other globals referenced by
    the __del__() method may already have been deleted. For this
    reason, __del__() methods should do the absolute minimum needed
    to maintain external invariants."

    in this case,

    def __del__(self):
    super(self.__class__, self).__del__()

    should do the trick.

    in other cases, you may have to store references to important globals to class
    or instance variables, or in bound arguments:

    def __del__(self, name=name):
    name(...)

    or you could just wrap the cleanup in a try/except clause, since this only happens
    when the interpreter is shutting down.

    for cleanup details, see:

    http://www.python.org/doc/essays/cleanup.html

    </F>
    Fredrik Lundh, Feb 16, 2005
    #1
    1. Advertising

  2. Fredrik Lundh

    Duncan Booth Guest

    Fredrik Lundh wrote:

    > in this case,
    >
    > def __del__(self):
    > super(self.__class__, self).__del__()
    >
    > should do the trick.


    Only if nobody ever tries to subclass your class, and if they aren't going
    to subclass it why bother to use super in the first place.

    >>> class Base(object):

    def __del__(self):
    print "Base.__del__"



    >>> class A(Base):

    def __del__(self):
    print "A.__del__"
    super(self.__class__, self).__del__()


    >>> class B(A):

    def __del__(self):
    print "B.__del__"
    super(self.__class__, self).__del__()


    >>> a = A()


    >>> del a

    A.__del__
    Base.__del__
    >>>
    >>> b = B()
    >>> del b

    B.__del__
    A.__del__
    A.__del__
    A.__del__
    A.__del__
    A.__del__
    .... and so on ...

    I don't see any easy way to ensure that the __del__ method gets passed up
    the chain safely.
    Duncan Booth, Feb 16, 2005
    #2
    1. Advertising

  3. Fredrik Lundh

    Ola Natvig Guest

    Duncan Booth wrote:
    > Fredrik Lundh wrote:
    >
    >
    >>in this case,
    >>
    >> def __del__(self):
    >> super(self.__class__, self).__del__()
    >>
    >>should do the trick.

    >
    >
    > Only if nobody ever tries to subclass your class, and if they aren't going
    > to subclass it why bother to use super in the first place.
    >
    >
    >>>>class Base(object):

    >
    > def __del__(self):


    There should be a super(self.__class__, self)._del__() here if I'm not
    totaly wong, which could be the case here ;)


    > print "Base.__del__"
    >
    >


    --
    --------------------------------------
    Ola Natvig <>
    infoSense AS / development
    Ola Natvig, Feb 16, 2005
    #3
  4. Fredrik Lundh

    Duncan Booth Guest

    Ola Natvig wrote:

    >> def __del__(self):

    >
    > There should be a super(self.__class__, self)._del__() here if I'm not
    > totaly wong, which could be the case here ;)
    >
    >
    >> print "Base.__del__"
    >>
    >>

    >


    There was one, but for some reason you trimmed it out of your quote:

    The original code before you trimmed it was:

    >>>> class B(A):

    > def __del__(self):
    > print "B.__del__"
    > super(self.__class__, self).__del__()
    Duncan Booth, Feb 16, 2005
    #4
  5. Fredrik Lundh

    Brian Beck Guest

    Duncan Booth wrote:
    > There was one, but for some reason you trimmed it out of your quote:
    >
    > The original code before you trimmed it was:


    Look carefully, he was commenting on the contents of class Base (which
    does omit the line he suggests), not class B. Whether he's correct or
    not, I'm not wizardly enough to comment on.

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Feb 16, 2005
    #5
  6. Fredrik Lundh

    Duncan Booth Guest

    Ola Natvig wrote:
    > Duncan Booth wrote:
    >>>>>class Base(object):

    >>
    >> def __del__(self):

    >
    > There should be a super(self.__class__, self)._del__() here if I'm not
    > totaly wong, which could be the case here ;)
    >
    >
    >> print "Base.__del__"
    >>
    >>

    >


    Thanks to Brian Beck for pointing out I hadn't read your question
    correctly.

    There must not be a super call from the class Base. This is a common
    problem when using super: if the method you are propagating isn't defined
    by 'object' (and most aren't), then you must provide some way to terminate
    the chain of calls. One way to do this is to ensure that you have some base
    class which does not attempt to pass the call upwards. Then all you have to
    do is ensure that everything that has the method is a subclass of Base and
    this will ensure that the Base method will be the last method called in the
    super chain.

    If I had included a super call:

    >>> class Base(object):

    def __del__(self):
    print "Base.__del__"
    super(self.__class__, self).__del__()


    >>> x = Base()
    >>> del x

    Base.__del__
    Exception exceptions.AttributeError: "'super' object has no attribute
    '__del__'" in <bound method Base.__del__ of <__main__.Base object at
    0x00B43D90>> ignored
    >>>
    Duncan Booth, Feb 16, 2005
    #6
    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. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,762
    Darryl Pierce
    Feb 24, 2005
  2. Christopher J. Bottaro

    super not working in __del__ ?

    Christopher J. Bottaro, Feb 15, 2005, in forum: Python
    Replies:
    0
    Views:
    319
    Christopher J. Bottaro
    Feb 15, 2005
  3. Christopher J. Bottaro

    Re: super not working in __del__ ?

    Christopher J. Bottaro, Feb 16, 2005, in forum: Python
    Replies:
    5
    Views:
    771
    Jeff Shannon
    Feb 18, 2005
  4. Replies:
    7
    Views:
    450
    Patricia Shanahan
    Apr 6, 2008
  5. Hugh Sasse Staff Elec Eng

    Inheriting variables, super, and "not super"?

    Hugh Sasse Staff Elec Eng, Dec 9, 2003, in forum: Ruby
    Replies:
    7
    Views:
    123
    Hugh Sasse Staff Elec Eng
    Dec 11, 2003
Loading...

Share This Page