On Sat, Mar 13, 2010 at 8:19 AM, Jon Clements <span dir="ltr"><<a href="mailto:joncle@googlemail.com">joncle@googlemail.com</a>></span> wrote:<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


The name 'some_function' is completely redundant -- don't need it,<br>
don't actually care about the function afterwards, as long as it<br>
becomes a __call__ of a 'B' *instance*.<br></blockquote><div><br></div><div>Special methods are looked up on the class, not the instance, so you can't dot his. However, from reading this thread, I think you're just a bit hung up on naming: you don't need these random / arbitrary functions to be the __call__ on an instance.</div>

<div><br></div><div>You need these functions to be called when said instance is called.</div><div><br></div><div>The easiest way to do that is simply define on the class:</div><div><br></div><div>def __call__(self, *args, **kwargs):</div>

<div>    self.func(*args, **kwargs)</div><div><br></div><div>Then while you are iterating over your list of a thousand functions and making instances, just assign each instance's func attribute.</div><div><br></div><div>

So,</div><div><br></div><div>for fn in [function_one, function_two, function_three, function_four]:</div><div>    inst = B()</div><div>    inst.func = fn</div><div><br></div><div>Now, if you really like the decorator syntax, sure:</div>

<div><br></div><div><div>>> def inject(klass):</div><div>...     def decorator(fn):</div><div>...             inst = klass()</div><div>...             inst.func = fn</div><div>...             return inst</div><div>
...     return decorator</div>
<div>... </div><div>>>> class A:</div><div>...     def __init__(self):</div><div>...             pass</div><div>...     def __call__(self, *args, **kwargs):</div><div>...             self.func(*args, **kwargs)</div>

<div>... </div><div>>>> @inject(A)</div><div>... def test(mmm):</div><div>...     print mmm</div><div>... </div><div>>>> test</div><div><__main__.A instance at 0x1004a4fc8></div><div>>>> test("Hello")</div>

<div>Hello</div><div><br></div><div>Now, I don't -fully- understand what you're trying to do so this may not be precisely the right thing, but you should be able to adapt the theory.</div></div><div><br></div></div>

<div name="mailplane_signature">--S</div>