<div dir="ltr">Maybe I'm missing something, but what's the use case, and why aren't plain old decorators suitable?<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jan 23, 2014 at 10:31 AM, Suresh V. <span dir="ltr"><<a href="mailto:suresh_vv@yahoo.com" target="_blank">suresh_vv@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Nicely done :-)<br>
<br>
"foo" may come from a library or something, so rather than a decorator we may have to monkey patch it. Unless there is a nicer solution.<br>
<br>
Will functools be a good place for something like this?<div class="HOEnZb"><div class="h5"><br>
<br>
On Thursday 23 January 2014 01:50 PM, Chris Angelico wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Jan 23, 2014 at 7:11 PM, Suresh V. <<a href="mailto:suresh_vv@yahoo.com" target="_blank">suresh_vv@yahoo.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thursday 23 January 2014 01:22 PM, Chris Angelico wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
On Thu, Jan 23, 2014 at 6:20 PM, Suresh V. <<a href="mailto:suresh_vv@yahoo.com" target="_blank">suresh_vv@yahoo.com</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Can we add these two attributes for every function/method where each is a<br>
list of callables with the same arguments as the function/method itself?<br>
<br>
Pardon me if this has been discussed before. Pointers to past discussions<br>
(if any) appreciated.<br>
</blockquote>
<br>
<br>
I'm not exactly sure what you're looking for here. What causes a<br>
callable to be added to a function's __before__ list, and/or what will<br>
be done with it?<br>
</blockquote>
<br>
<br>
These are modifiable attributes, so something can be added/deleted from the<br>
__before__ or __after__ lists.<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
If you mean that they'll be called before and after the function<br>
itself, that can be more cleanly done with a decorator.<br>
</blockquote>
<br>
<br>
Yes. Each item in the list will be called in order immediately before/after<br>
each invocation of the function. This is kinda like decorators, but more<br>
flexible and simpler. Scope for abuse may be higher too :-)<br>
</blockquote>
<br>
def prepostcall(func):<br>
def wrapper(*args,**kwargs):<br>
for f in wrapper.before: f(*args,**kwargs)<br>
ret = func(*args,**kwargs)<br>
for f in wrapper.after: f(*args,**kwargs)<br>
return ret<br>
wrapper.before = []<br>
wrapper.after = []<br>
return wrapper<br>
<br>
@prepostcall<br>
def foo(x,y,z):<br>
return x*y+z<br>
<br>
foo.before.append(lambda x,y,z: print("Pre-call"))<br>
foo.after.append(lambda x,y,z: print("Post-call"))<br>
<br>
Now just deal with the question of whether the after functions should<br>
be called if the wrapped function throws :)<br>
<br>
</blockquote>
<br>
<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
ChrisA<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/<u></u>codeofconduct/</a><br>
<br>
</blockquote>
<br>
<br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/<u></u>codeofconduct/</a><br>
</div></div></blockquote></div><br></div>