Hi, I'd like to write some class that can help me build reusable formula easily, some simple code like this.
# -*- coding: utf8 -*- class OperationResult: def __init__(self, left, right): self.dataSource = dataSource def __add__(self, other): self.dataSource.stack.append('+') return self def __div__(self, other): self.dataSource.stack.append('/') return self class Operand: def __init__(self, dataSource, name): self.dataSource = dataSource self.name = name def __add__(self, other): self.dataSource.stack.append(self) self.dataSource.stack.append(other) self.dataSource.stack.append('+') return OperationResult(self.dataSource) class DataSource: def __init__(self): self.stack = [] def __getitem__(self, key): return Operand(self, key) def ROE(dataSource): a = dataSource[u'股東權益總額'] b = dataSource[u'每股淨額'] c = dataSource[u'當季平均市價'] return (a + b) / c Now I can use ROE formula easily, it will return a object that contain formula stack like this [a, b, +, c, /] And then I can get real data of those items from database or other place. The formula depends on nothing. It don't care where to get data and how to calculate it. I can reuse it easily. Now, here comes the problem : I have to override all the operation methods, such as __add__ and __mul__. I know the most stupid way is just to write all of them like this. def __add__(self, other): self.leftOperation('add', other) def __mul__(self, other): self.leftOperation('mul', other) But I don't want to do that in this stupid way. I want to use little code to redirect these operation methods to some specific method with it's name. What can I do? Thanks. -- http://mail.python.org/mailman/listinfo/python-list