On Sat, Mar 13, 2010 at 11:19 AM, Jon Clements <jon...@googlemail.com> wrote: > This is semi-experimental and I'd appreciate opinions of whether it's > the correct design approach or not. It seems like a good idea, but it > doesn't mean it is. > > I have a class 'A', this provides standard support functions and > exception handling. > I have 'B' and 'C' which specialise upon 'A' > > What I'd like to achieve is something similar to: > > @inject(B): > def some_function(a, b): > pass # something useful > > The name 'some_function' is completely redundant -- don't need it, > don't actually care about the function afterwards, as long as it > becomes a __call__ of a 'B' *instance*. > > I've basically got a huge list of functions, which need to be the > callable method of an object, and possibly at run-time, so I don't > want to do: > > class Something(B): > def __call__(self, etc.. etc...): > pass # do something > > I've got as far as type(somename, (B,), {}) -- do I then __init__ or > __new__ the object or... > > In short, the function should be the __call__ method of an object that > is already __init__'d with the function arguments -- so that when the > object is called, I get the result of the the function (based on the > objects values).
I'm not sure exactly what you are asking for, but if what you want is a bunch of different objects that vary only by their class's __call__ you could do it with a function that returns a new class based on A but with a new __call__: def make_new_call_class(base_class, call_func): class NewClass(base_class): def __call__(self, *args, **kw): return call_func(self, *args, *kw) return NewClass or the return could even be NewClass() [return an instance] if this is a one off. That said, I'm not really sure what this behavior is good for. -Jack -- http://mail.python.org/mailman/listinfo/python-list