[Edu-sig] COUNTER class.
Jeffrey Elkner
jeff@elkner.net
27 Feb 2002 12:25:24 -0500
Hi Kirby,
With the help of student Python guru, Lex Berezhny, I've been playing
around a bit with the suggestion you sent me.
Here is what I get when I attempt to merge the "Element" class you sent
me with a "countable thing" class that Lex showed me:
class COUNTER:
stat_state = {}
def __init__(self, value = 0, type):
self.value = value
if not self.stat_state.has_key(type):
self.stat_state[type] = stat_state = {"assign": 0,
"compare": 0}
self.stat = self.stat_state[type]
def __repr__(self):
return str(self.value)
def __eq__(self, other):
self.state["compare"] += 1
return self.value == other.value
def __gt__(self, other):
self.state["compare"] += 1
return self.value > other.value
def __lt__(self, other):
self.state["compare"] += 1
return self.value < other.value
def __le__(self, other):
self.state["compare"] += 1
return self.value <= other.value
def __ge__(self, other):
self.state["compare"] += 1
return self.value >= other.value
The idea is to create a class of things that know how to count together,
so that they increment a common counter (so a > b should only count 1
comparison).
I couldn't find a way to give this property to assignment. Every time an
assignment happens with one of these things, I would like to count it.
Is there a way to do that?
jeff