how to create local dynamic varables

Posted by xielingyun on Stack Overflow See other posts from Stack Overflow or by xielingyun
Published on 2012-11-20T10:57:27Z Indexed on 2012/11/20 11:00 UTC
Read the original article Hit count: 285

Filed under:
|
|

this is my code, i want to use eval() to get the rule status and eval() nead local varables, there is many classes inherit class base, so i should to rewrite get_stat() in every class.i just want to avoid this, an idea is to create dynamic varables in get_stat(),eg. in class b it dynamic create var a and b
how to 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)

© Stack Overflow or respective owner

Related posts about dynamic

Related posts about python-3.x