[Tutor] How to delete a class instance

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Mar 16 18:10:51 CET 2005



On Wed, 16 Mar 2005, Shitiz Bansal wrote:

> No, this list is not a linked list.
> Since mu original code is rather huge, I am presenting
> the relevant snippet.
> queue=[]
> def genpassenger(num,destination,queue=queue):
>     for i in range(num):
>         newpass=passenger(destination)
>         queue.append(newpass)
>         newpass.start()
> class passenger(threading.Thread):
>         def __init__
>         <snip>
>         def run:
>             #do something
>             delete itself
>
> genpassengers is called interactively by the user


Ok, this makes sense.  Each passenger thread needs to know about the
queue, because that's the place you want the passenger to drop out of.

Lists support a 'remove()' method, so you may be able to use it.


> 2) If i used a linked list, does automatic memory handling by python
> entail that delete itself is automatically accomplished once i have
> readjusted the pointers, since no further reference to the instance
> remains.

Yes.


> 3)  How about making a destructor function (on lines of c++) and
> deleting all the variables of the class.

This is usually unnecessary, as the same garbage collection that recycles
the class does the same to attributes of the class.


If you have more questions, please feel free to ask!



More information about the Tutor mailing list