On 22 Feb 2006 17:28:35 -0800 "Russ" <[EMAIL PROTECTED]> wrote: > I would like to let the user of one of my classes > "configure" it by activating or de-activating a particular > behavior (for all instances of the class).
> One way to do this, I figured, is to have a static class > variable, along with a method to set the variable. > However, I am stumped as to how to do that in python. > Suggestions welcome. Like this? >>> class Switchable(object): ... def _plus(self, val): ... return val ... def _minus(self, val): ... return -val ... switch = _plus ... def sense(self, value): ... return self.switch(value) ... >>> a = Switchable() >>> b = Switchable() >>> a.sense(2) 2 >>> b.sense(3) 3 >>> Switchable.switch = Switchable._minus >>> a.sense(3) -3 >>> b.sense(2) -2 Seems to work. Might be more elegant to do that switch with a method of Switchable. Maybe: def flipswitch(self, num): if num == 1: self.switch = self._plus else: self.switch = self._minus Then you'd switch with, e.g.: Switchable.flipswitch(1) > The next best thing, I figure, is to just use a global > variable. Several "methods" of the class then check the > value of the global variable to determine what to do. The > user can then just set the variable to get the desired > behavior. Ick. > However, I tried this and it does not seem to work. I > imported the class, then set the global variable. But the > new value of the variable somehow did not get back into > the class methods that need to see it. Undoubtedly you shadowed it somehow, but without seeing the code, I can't guess how. > Can anyone give me a clue about this? If there is a better > way, please let me know. Thanks. Recommend you stick with the first idea, which as you see works fine. Cheers, Terry -- Terry Hancock ([EMAIL PROTECTED]) Anansi Spaceworks http://www.AnansiSpaceworks.com -- http://mail.python.org/mailman/listinfo/python-list