[Python-ideas] __before__ and __after__ attributes for functions

Andrew Barnert abarnert at yahoo.com
Thu Jan 23 19:10:33 CET 2014


On Jan 23, 2014, at 7:12, Chris Angelico <rosuav at gmail.com> wrote:

> On Fri, Jan 24, 2014 at 2:08 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>>    # XXX should the "after" function also receive the return value?
> 
> That's a possible consideration, but it messes up the "has the same
> arguments" bit. Plus, what happens to the after function(s) if the
> main function throws an error? (And what happens to the main if a
> before function bombs?) Very hard to solve in the general case, which
> is a good reason for this NOT to go into the stdlib, but just to be
> implemented whenever it's wanted.

There _might_ be good, usually-right answers to these questions.

But the only way we're likely to find them is if someone puts it up on PyPI and people start using it, not by guessing a priori. Which is another good reason not to go straight for the stdlib.

And a PyPI module can go crazy with options: have after functions that do or don't get the result based on an arg to the decorator, and that do or don't replace the result, and before functions that can return replacement args, and after_except functions that run on exception, get the exception, and can raise or return (think of deferred chaining options), or whatever else you can think of.


More information about the Python-ideas mailing list