[Tutor] How do I destroy class instances ?

Alan Gauld alan.gauld at btinternet.com
Sun Feb 24 18:48:51 CET 2008


"dave selby" <dave6502 at googlemail.com> wrote

>I have created a list of class instances, works a treat. I need to be
> able to re __init__ the instances on a SIGHUP so I guess the best 
> way
> is to destroy them & re make them.

You could just call init on them...

>>> class C:
...   def __init__(s,x,y):
...     s.x = x
...     s.y = y
...   def __str__(s): return 'x,y = %s, %s' % (s.x, s.y)
...
>>> for n in range(3): L.append(C(n,n+1))
...
>>> for c in L: print c
...
x,y = 0, 1
x,y = 1, 2
x,y = 2, 3
>>> #   -- NOW CALL INIT
>>> for c in L: C.__init__(c,42,66)
>>> # or it could be
>>> # for c in L: c.__init__(42,66)    if you prefer
...
>>> for c in L: print c
...
x,y = 42, 66
x,y = 42, 66
x,y = 42, 66
>>>

> err ... how do I destroy an instance ?

Stop anything referring to it:

In the above:

L = []

will delete all the items in L - provided nothing else refers to them

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
Temorarily at:
http://uk.geocities.com/alan.gauld@btinternet.com/
Normally:
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list