this is my code, I want to use eval() to get the rule status but eval() needs local variables, there is many classes that inherits the base class, so I need to rewrite get_stat() in every class.
I want to avoid this, an idea is to create dynamic variables in get_stat(),eg. class b dynamically creates variables a and b in func get_stat()
How should I create dynamic varables in function? or any other way to avoid this stupid idea. I use python 3.2.3, locals() does not work
class base(object):
def check(self):
stat = get_stat()
def get_stat(self):
pass
class b(base):
rule = 'a > 5 and b < 3'
a = 0
b = 0
def update_data(self, a, b):
self.a = a
self.b = b
def get_stat(self):
a = self.a
b = self.b
return eval(rule)
class b(base):
rule = 'd > 5 and e < 3'
d = 0
e = 0
def update_data(self, d, e):
self.d = d
self.e = e
def get_stat(self):
d = self.d
e = self.e
return eval(rule)
rulefor every class, you might as well just re-write the functionruleattribute of each subclass can be changed on-the-fly andget_stat()will return results based on it's current value. You're right about the fact thatrulecould just as well be made a method, although the syntax needed to change it might not be as convenient or clean looking as would be this way.