deleting an object

Terry Reedy tjreedy at udel.edu
Fri Sep 26 02:59:33 EDT 2008


Karl Kobata wrote:
> Please help.  Questions based on snippet of code below.

Please post code that works.  Use cut and paste.

> 1)       when myTestCase is deleted, is just the pointer deleted or the 
> entire instance with all of its data and data structure deleted?

Have you read the manual section on the del statement?

> 2)       What is the practice for deleted the object and recovering the 
> memory it occupies?

Most programs use del rather sparingly.  Most temporary objects are 
created within functions and get unbound automatically when the function 
exits.  If you need to guarantee release of external resources before 
the program ends, make explict calls to .close methods of the instances 
that connect to them.  Actually memory recovery depends on the 
implementation and version.  I would not worry about this until you 
really need to.

> 3)       If delete is the way, what happens to 
> ‘testUtils.utilFunction1()’ if it were instantiated in other classes 
> that have not been deleted yet?

Objects will not be destroyed while still reachable from code (in the 
absence of a severe bug).

> import testUtils
> class testClass:
>             def __init__(self):
>                         self.testList = list()
>                         self.testDict1 = dict()
>                         self.utils1 = testUtils.utilFunction1()

>          myTestCase = testClass()
>          delete myTestCase

Don't indent main body code.
Do use legal keywords.

tjr




More information about the Python-list mailing list