[Tutor] How do I destroy class instances ?
Dave Kuhlman
dkuhlman at rexx.com
Sun Feb 24 19:13:06 CET 2008
On Sun, Feb 24, 2008 at 04:14:02PM +0000, dave selby 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.
>
> err ... how do I destroy an instance ?
It is not likely that you need to.
When your instances are no longer referenced (nothing points to
them, so to speak), they will be garbage collected. Here is an
illustraction -- This code collects three instances of class
MyClass in a list, then later "forgets" them:
myinstances = []
myinstances.append(MyClass())
myinstances.append(MyClass())
myinstances.append(MyClass())
o
o
o
myinstances = []
Or, if there are resources that are held onto by each instance and
need to be cleaned up, for example an open file. Then do something
like the following:
for inst in myinstances:
inst.cleanup()
myinstances = []
where "cleanup" is a method implemented in each class that needs to
cleanup/release resources.
But, remember that, if there are objects that are referred to by
your instances and *only* your instances, then when your instances
go away (are garbage collected), those other things will also
automatically go away, too. No extra work is needed.
It takes a little thought before you will figure out that this is
something that needs (almost) no thought.
Hope this helps.
- Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list