I have a class factory problem. You could say that my main problem is that I don't understand class factories. My specific problem: I have a class with several methods I've defined. To this class I want to add a number of other class methods where the method names are taken from a list. For each list member, I want to build a method having the list member name so that I can override another method of the same name for objects which are instances of this class. Finally, I want my class factory which creates the method to call the overridden method.
I'm sure my description is confusing, so I'll try to illustrate it.
import Numeric # The Numeric module contains a whole lot of methods which operate on certain number types
class foo: """ this is a number type class """ def __init__(self, value): self.value = float(value) def __str__(self): . . other methods here .
import string ufuncs = string.split(""" sqrt arccos arccosh arcsin arcsinh arctan arctanh cos cosh tan tanh log10 sin sinh sqrt absolute fabs floor ceil fmod exp log conjugate """) # these are the methods from Numeric that I want to override/wrap for my number type
for u in ufuncs: I need something here which builds methods called sqrt(), arccos(), etc.
An illustrative example of the class methods I want to build with some sort of class factory is def sqrt(self): val = Numeric.sqrt(self.value) return foo(Numeric.sqrt(self.value) + 42)
def arccos(self): val = Numeric.arccos(self.value) return foo(Numeric.arccos(self.value) + 42)
if __name__ == "__main__": a = 9 b = foo(7) print Numeric.sqrt(a) print Numeric.sqrt(b)
This would print 3 7
Note that the methods I want to create under program control are all identical in form. In this example they all call the wrapped method with an argument 42 greater than the value of the number. Does anyone have an example where they've done something similar or could help me out with an example? thanks in anticipation, Gary -- http://mail.python.org/mailman/listinfo/python-list