Deleting an object within itself.

Michael Kuperstein - EECS (mkuperst@dryden.eecs.wsu.edu)
Wed, 27 Jul 1994 18:08:22 GMT

In short, how do you do it?

Here is the program:

class Hello:
def __init__( self ):
print 'Hello.__init__() called'

def __del__( self ):
print '__del__ called'

def hi( self ):
print 'Hi there!'

def Delete( self ):
del self

def test():
print 'instantiating h'
h = Hello()
print 'deleting h'
h.Delete
print 'accessing h'
h.hi()
print 'exiting test()'

And here is the output:

dryden% python
Python 1.0.2 (Jun 21 1994)
Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>> import delete
>>> delete.test()
instantiating h
Hello.__init__() called
deleting h
accessing h
Hi there!
exiting test()
__del__ called
>>>

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Michael Kuperstein              \(*)|\
mkuperst@eecs.wsu.edu            \ --_\o   --   Ahhhhhhhhhhhhhhh!!!!!!!
Major: Computer Science           \(*)!