[Tutor] destroying classes

Rob McGee i812@iname.com
Sun, 2 Dec 2001 16:18:27 -0600


First some amusement:
{fun}
On Sat, Dec 01, 2001 at 08:32:20PM -0800, Kirby Urner wrote about
    "Re: [Tutor] further ignorant babbling":
> At 09:20 PM 12/1/2001 -0500, Kirk Bailey wrote:
> >OK, newest and latest shot at looking like an idiot in
> >front of my betters.
> 
> We're all idiots of one kind or another, not to worry.

Kirk wasn't saying we're "better" than him. It was just a typo. He meant
to say "bettors". See, a bunch of us were making some wagers on the side
about when Kirk would finish his program. :)

So okay everybody, listen up: the bets are off. Somebody must have told
Kirk about the pool. All bets will be returned to bettors -- of course
the house will retain its 10% cut. :)
{/fun}

But I don't want to waste bandwidth on a list this busy with pure
silliness, so I have some questions. You may recall my "class struggle"
thread from a couple of days ago. All is going quite well in that
project, and class namespaces are making the result much better than its
classless predecessor.

Anyway, one of my classes has a method which should result in the
destruction of the class instance. But the method also wants to return a
value. If I put a "del self" command in there, would the method end at
that point, or would it continue to completion?

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

algebra = InSession('algebra')

reportCard = algebra.dismissal('D')
print reportCard
{/code}

The code above works, but the "algebra" instance of InSession still
exists. I changed the "del self" to "exec('del ' + self.name)", and sure
enough it kills the instance, but it doesn't return the value (something
like a NameError exception because "algebra" doesn't exist.)

Would I be able to do this all in the __del__() method instead? If so I
don't have to do anything other than "del algebra"? Can I give my method
values like "def __del__(self, grade):"? How would I pass the grade
variable? And to assign a value to a variable, would it be like this:
    reportCard = algebra.__del__('F')
Would that delete the instance?

Anyway, I've tried it, and I can't figure out how to delete the instance
and return a value at the same time. Using that last example of
__del__(), there seems to be nothing special about the __del__ name.
The string is returned, but "algebra" still exists. I guess I'm going to
have to do this from outside the function ...
    reportCard = algebra.dismissal("C")
    del algebra

Now moving on, I will have saved pointers to this instance in instances
of another class. Suppose I have a School instance called "highSchool",
and one attribute of that class instance is "highSchool.subject =
algebra". And another might be "highSchool.algebra = 'math'". What will
happen to those highSchool attributes when algebra is dismissed?

Thanks again -- this list (and Python itself) is great.

    Rob - /dev/rob0