How do I define a __del__ method for an object?

Discussion in 'Python' started by Stewart Midwinter, Mar 7, 2004.

  1. I was looking through Swaroop's tutorial, "A Byte of Python", on Python
    objects,(http://www.python.g2swaroop.net/), and he mentions that you can
    define a __del__ function to completely delete object instances.
    However, when I try to define one in the attached script, not just one,
    but all, of the defined objects disappear.

    Can someone show me how to define a __del__ object that works on only
    one object instance?

    Thanks
    Stewart

    #!/usr/bin/python
    # Filename: objvar.py
    # from Swaroop's A Byte of Python,
    # http://www.python.g2swaroop.net/byte/ch11s05.html

    class Person:
    '''class Person represents a person.'''
    population = 0

    def __init__(self, name):
    '''Initializes the person.'''
    self.name = name
    print '(Initializing %s)' % self.name

    # When this person is created,
    # he/she adds to the population
    Person.population += 1

    def sayHi(self):
    '''Method sayHi greets the other person.
    Really, that's all it does.'''
    print 'Hi, my name is %s.' % self.name


    def howMany(self):
    '''Prints the current population.'''
    # There will always be atleast one person
    if Person.population == 1:
    print 'I am the only person here.'
    else:
    print 'We have %s persons here.' % \
    Person.population

    def sayDie(self):
    '''when a person dies,
    she reduces the population by one,
    but she is still remembered.'''
    print self.name, 'has died.'
    Person.population -= 1



    swaroop = Person('Swaroop')
    swaroop.sayHi()
    swaroop.howMany()

    kalam = Person('Abdul Kalam')
    kalam.sayHi()
    kalam.howMany()

    swaroop.sayHi()
    swaroop.howMany()

    kalam.sayDie()
    kalam.howMany()
    #kalam.__del__('Abdul Kalam')
    abdul = Person('Abdul')
    abdul.sayHi()
    abdul.howMany()
    print kalam.sayHi.__doc__
    print Person.__doc__
     
    Stewart Midwinter, Mar 7, 2004
    #1
    1. Advertising

  2. Stewart Midwinter

    Uwe Grauer Guest

    Stewart,

    __del__ is used for object destruction.

    Read:

    http://python.org/doc/current/ref/customization.html#l2h-175

    Uwe

    Stewart Midwinter wrote:
    > I was looking through Swaroop's tutorial, "A Byte of Python", on Python
    > objects,(http://www.python.g2swaroop.net/), and he mentions that you can
    > define a __del__ function to completely delete object instances.
    > However, when I try to define one in the attached script, not just one,
    > but all, of the defined objects disappear.
    >
    > Can someone show me how to define a __del__ object that works on only
    > one object instance?
    >
    > Thanks
    > Stewart
    >
    >
    > ------------------------------------------------------------------------
    >
    > #!/usr/bin/python
    > # Filename: objvar.py
    > # from Swaroop's A Byte of Python,
    > # http://www.python.g2swaroop.net/byte/ch11s05.html
    >
    > class Person:
    > '''class Person represents a person.'''
    > population = 0
    >
    > def __init__(self, name):
    > '''Initializes the person.'''
    > self.name = name
    > print '(Initializing %s)' % self.name
    >
    > # When this person is created,
    > # he/she adds to the population
    > Person.population += 1
    >
    > def sayHi(self):
    > '''Method sayHi greets the other person.
    > Really, that's all it does.'''
    > print 'Hi, my name is %s.' % self.name
    >
    >
    > def howMany(self):
    > '''Prints the current population.'''
    > # There will always be atleast one person
    > if Person.population == 1:
    > print 'I am the only person here.'
    > else:
    > print 'We have %s persons here.' % \
    > Person.population
    >
    > def sayDie(self):
    > '''when a person dies,
    > she reduces the population by one,
    > but she is still remembered.'''
    > print self.name, 'has died.'
    > Person.population -= 1
    >
    >
    >
    > swaroop = Person('Swaroop')
    > swaroop.sayHi()
    > swaroop.howMany()
    >
    > kalam = Person('Abdul Kalam')
    > kalam.sayHi()
    > kalam.howMany()
    >
    > swaroop.sayHi()
    > swaroop.howMany()
    >
    > kalam.sayDie()
    > kalam.howMany()
    > #kalam.__del__('Abdul Kalam')
    > abdul = Person('Abdul')
    > abdul.sayHi()
    > abdul.howMany()
    > print kalam.sayHi.__doc__
    > print Person.__doc__
     
    Uwe Grauer, Mar 7, 2004
    #2
    1. Advertising

  3. Stewart Midwinter

    christofer Guest

    Stewart Midwinter <> wrote in message news:<>...
    > #!/usr/bin/python
    > # Filename: objvar.py
    > # from Swaroop's A Byte of Python,
    > # http://www.python.g2swaroop.net/byte/ch11s05.html
    >
    > class Person:
    > '''class Person represents a person.'''
    > population = 0
    >
    > def __init__(self, name):
    > '''Initializes the person.'''
    > self.name = name
    > print '(Initializing %s)' % self.name
    >
    > # When this person is created,
    > # he/she adds to the population
    > Person.population += 1
    >
    > def sayHi(self):
    > '''Method sayHi greets the other person.
    > Really, that's all it does.'''
    > print 'Hi, my name is %s.' % self.name
    >
    >
    > def howMany(self):
    > '''Prints the current population.'''
    > # There will always be atleast one person
    > if Person.population == 1:
    > print 'I am the only person here.'
    > else:
    > print 'We have %s persons here.' % \
    > Person.population
    >
    > def sayDie(self):
    > '''when a person dies,
    > she reduces the population by one,
    > but she is still remembered.'''
    > print self.name, 'has died.'
    > Person.population -= 1
    >
    >
    >
    > swaroop = Person('Swaroop')
    > swaroop.sayHi()
    > swaroop.howMany()
    >
    > kalam = Person('Abdul Kalam')
    > kalam.sayHi()
    > kalam.howMany()
    >
    > swaroop.sayHi()
    > swaroop.howMany()
    >
    > kalam.sayDie()
    > kalam.howMany()
    > #kalam.__del__('Abdul Kalam')
    > abdul = Person('Abdul')
    > abdul.sayHi()
    > abdul.howMany()
    > print kalam.sayHi.__doc__
    > print Person.__doc__
    >
    > --


    define this method (a destructor) in your class:

    def __del__(self):
    do whatever here...
     
    christofer, Mar 7, 2004
    #3
  4. Stewart Midwinter

    Peter Otten Guest

    Stewart Midwinter wrote:

    > I was looking through Swaroop's tutorial, "A Byte of Python", on Python
    > objects,(http://www.python.g2swaroop.net/), and he mentions that you can
    > define a __del__ function to completely delete object instances.
    > However, when I try to define one in the attached script, not just one,
    > but all, of the defined objects disappear.


    ?

    > Can someone show me how to define a __del__ object that works on only
    > one object instance?


    A minimal example:

    >>> class Person:

    .... def __init__(self, name):
    .... self.name = name
    .... Person.population += 1
    .... population = 0
    .... def __del__(self):
    .... Person.population -= 1
    ....
    >>> stuart = Person("Stuart")
    >>> Person.population

    1
    >>> del stuart
    >>> Person.population

    0
    >>> stuart = Person("Stuart")
    >>> midwinter = stuart
    >>> print Person.population

    1
    >>> del midwinter
    >>> Person.population

    1
    >>> del stuart
    >>> Person.population

    0

    The __del__() method is called after the last binding to the object is
    removed, i. e. the object becomes unreachable.

    Explicitly deleting bindings is not very common. __del__() calls rather
    occur when the last reference to an object goes out of scope:

    >>> def transient():

    .... p = Person("Peter")
    .... print Person.population
    ....
    >>> transient()

    1
    >>> Person.population

    0
    >>> mary = Person("Mary")
    >>> Person.population

    1
    >>> mary = None
    >>> Person.population

    0

    The caveat is that __del__() may not always be called immediately and
    sometimes never.

    Another point: Most objects do *not* need __del__() methods at all - I think
    the ratio in the Python library is about 4 to 100, so don't spend too much
    time on them.

    Peter

    PS: sayDie() is a badly chosen name as it pretends to inform the user while
    the population is actualy changed.
     
    Peter Otten, Mar 7, 2004
    #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. Jane Austine
    Replies:
    2
    Views:
    369
    Steven Taschuk
    Jul 2, 2003
  2. Jane Austine
    Replies:
    1
    Views:
    477
    Erik Max Francis
    Jul 2, 2003
  3. Baoqiu Cui
    Replies:
    4
    Views:
    536
    Baoqiu Cui
    Jan 31, 2005
  4. Benny
    Replies:
    3
    Views:
    116
    Mauricio Fernández
    Jul 13, 2004
  5. John J. Franey
    Replies:
    3
    Views:
    140
    George
    Jun 15, 2007
Loading...

Share This Page