[Python-ideas] __before__ and __after__ attributes for functions

Suresh V. suresh_vv at yahoo.com
Thu Jan 23 09:31:50 CET 2014


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/
>




More information about the Python-ideas mailing list