How do I define a __del__ method for an object?

  • Thread starter Stewart Midwinter
  • Start date
S

Stewart Midwinter

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__
 
C

christofer

Stewart Midwinter said:
#!/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...
 
P

Peter Otten

Stewart said:
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:
.... def __init__(self, name):
.... self.name = name
.... Person.population += 1
.... population = 0
.... def __del__(self):
.... Person.population -= 1
....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:
.... p = Person("Peter")
.... print 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top