[Python-ideas] __before__ and __after__ attributes for functions

Suresh V. suresh_vv at yahoo.com
Thu Jan 23 10:52:56 CET 2014


On Thursday 23 January 2014 02:57 PM, David Townshend wrote:

>
> Not a decorator, but you can monkey patch it:
>
>      @wraps(smtplib.SMTP.sendmail)
>      def sendmail(*args, **kwargs):
>          other_function()
>          return smtplib.SMPT.sendmail(*args, **kwargs)
>
>      smtplib.SMTP.sendmail = sendmail
>

Correct. I want to say something like:

from functools import prepostcall
smtplib.SMTP.sendmail = prepostcall(smtplib.SMTP.sendmail)
smtplib.SMTP.sendmail.before.append(other_function)

This seems less error-prone. And more conducive to multiple patching.




More information about the Python-ideas mailing list