[Tutor] How do I destroy class instances ?
dave selby
dave6502 at googlemail.com
Sun Feb 24 22:59:38 CET 2008
On 24/02/2008, Dave Kuhlman <dkuhlman at rexx.com> wrote:
> 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.
Thanks for replying, I have been away from Python for quite a while,
being dabbleing in C++ on a KDE4 app. I Forgot just how friendly
Python is :)
Cheers
Dave
>
> Hope this helps.
>
> - Dave
>
>
> --
> Dave Kuhlman
> http://www.rexx.com/~dkuhlman
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
More information about the Tutor
mailing list