[Python-ideas] __before__ and __after__ attributes for functions

David Mertz mertz at gnosis.cx
Thu Jan 23 19:31:45 CET 2014


On Thu, Jan 23, 2014 at 10:17 AM, Chris Angelico <rosuav at gmail.com> wrote:

> On Fri, Jan 24, 2014 at 5:14 AM, David Mertz <mertz at gnosis.cx> wrote:
> > from library import foo
> > @prepostcall
> > def foo(*args, **kws):
> >     return foo(*args, **kws)
>
> That's going to infinite-loop, so you'd need to do an 'as' import:
>
> from library import foo as foo_original
> @prepostcall
> def foo(*args, **kws):
>     return foo_original(*args, **kws)
>
> Of course, this assumes you want to do a 'from' import in the first
> place, rather than the more common approach of referencing
> 'library.foo()' - if the latter, then it is monkeypatching you need.
>

All true.  For some reason I was thinking of the timing of the binding
wrongly re. the infinite-loop. But yes, obviously using a different name in
an 'as' import solves that.


>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140123/e60dae0f/attachment.html>


More information about the Python-ideas mailing list