[Tutor] destroying classes

alan.gauld@bt.com alan.gauld@bt.com
Mon, 3 Dec 2001 13:45:08 -0000

> class InSession:
>   def __init__(self, name):
>     self.name = name
>   def dismissal(self, grade):
>     reportCard = 'You get a grade of ' + grade
>     del self
>     return reportCard

Getting objects to kill themselves in mid stream is not 
a good idea! Its a bit like asking your manservant to kill 
himself and then serve you dinner - tricky...

Better to send the kill from outside:
> reportCard = algebra.dismissal('D')
> print reportCard

> I changed the "del self" to "exec('del ' + self.name)", 

Boy, you really dig those exec statements :-)

> enough it kills the instance, but it doesn't return the value 

Coz its dead and gone before it hits the return...

> I'm going to have to do this from outside the function ...
>     reportCard = algebra.dismissal("C")
>     del algebra

Yes thats much nicer. You tell the object what to do and 
it does it - including killing itself.

Alan G.