[Python-ideas] __before__ and __after__ attributes for functions
Terry Reedy
tjreedy at udel.edu
Thu Jan 23 11:56:16 CET 2014
On 1/23/2014 3:31 AM, Suresh V. wrote:
Top-posting make posts/threads somewhat harder to follow for readers. A
decorators is simply a function named before a function def that is
called on the resulting function after the function is called. In other
words, it is purely syntactic sugar and
@prepostcall
def foo(x,y,z):
return x*y+z
is equivalent to
def foo(...
foo = prepostcall(foo)
For builtins, call the decorator function directly on the builtin. In
other words, use the last line of the equivalent.
int = prepostcall(int)
or use another name if you do not want to mask int.
> Nicely done :-)
>
> "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.
>
> Will functools be a good place for something like this?
>
> On Thursday 23 January 2014 01:50 PM, Chris Angelico wrote:
>> On Thu, Jan 23, 2014 at 7:11 PM, Suresh V.
>> <suresh_vv at yahoo.com> wrote:
>>> On Thursday 23 January 2014 01:22 PM, Chris Angelico wrote:
>>>>
>>>> On Thu, Jan 23, 2014 at 6:20 PM, Suresh V.
>>>> <suresh_vv at yahoo.com> wrote:
>>>>>
>>>>> Can we add these two attributes for every function/method where
>>>>> each is a
>>>>> list of callables with the same arguments as the function/method
>>>>> itself?
>>>>>
>>>>> Pardon me if this has been discussed before. Pointers to past
>>>>> discussions
>>>>> (if any) appreciated.
>>>>
>>>>
>>>> I'm not exactly sure what you're looking for here. What causes a
>>>> callable to be added to a function's __before__ list, and/or what will
>>>> be done with it?
>>>
>>>
>>> These are modifiable attributes, so something can be added/deleted
>>> from the
>>> __before__ or __after__ lists.
>>>
>>>
>>>>
>>>> If you mean that they'll be called before and after the function
>>>> itself, that can be more cleanly done with a decorator.
>>>
>>>
>>> Yes. Each item in the list will be called in order immediately
>>> before/after
>>> each invocation of the function. This is kinda like decorators, but more
>>> flexible and simpler. Scope for abuse may be higher too :-)
>>
>> def prepostcall(func):
>> def wrapper(*args,**kwargs):
>> for f in wrapper.before: f(*args,**kwargs)
>> ret = func(*args,**kwargs)
>> for f in wrapper.after: f(*args,**kwargs)
>> return ret
>> wrapper.before = []
>> wrapper.after = []
>> return wrapper
>>
>> @prepostcall
>> def foo(x,y,z):
>> return x*y+z
>>
>> foo.before.append(lambda x,y,z: print("Pre-call"))
>> foo.after.append(lambda x,y,z: print("Post-call"))
>>
>> Now just deal with the question of whether the after functions should
>> be called if the wrapped function throws :)
>>
>
>
>
>
>> 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/
>>
>
>
> _______________________________________________
> 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/
>
--
Terry Jan Reedy
More information about the Python-ideas
mailing list