[Edu-sig] Gutless classes

Kirby Urner urnerk at qwest.net
Fri Sep 9 19:54:01 CEST 2005

> class RunOnce:
>     def run(self):
>         self.run = _noMoreRunning
>         # do other stuff that you only want to happen once
>     def _noMoreRunning(self):
>         raise Exception("Can only call run() once.")

Yeah, this is clever, I like it.  You slam the door from the inside,
interpose a "door closed" next time run() is called -- likely in error.

A mutable runtime strategy might be to force oneself to create and run all
the instances needed, then shut 'em all down at the class level:

 class Runner (object):
    def run(self):
       print "Yo!"

 >>> obja = Runner()
 >>> objb = Runner()
 >>> obja.run()

 >>> def newrun(self):  raise Exception("Sorry, too late")

 >>> Runner.run = newrun
 >>> objb.run()

 Traceback (most recent call last):
   File "<pyshell#60>", line 1, in -toplevel-
   File "<pyshell#58>", line 1, in newrun
     def newrun(self):  raise Exception("Sorry, too late")
 Exception: Sorry, too late


More information about the Edu-sig mailing list