[Python-ideas] __before__ and __after__ attributes for functions

Suresh V. suresh_vv at yahoo.com
Fri Jan 24 08:54:07 CET 2014


On Friday 24 January 2014 10:39 AM, Ethan Furman wrote:
> On 01/23/2014 08:09 PM, Suresh V. wrote:
>>
>> Also it would mean that the client code imports from this package.
>> I would like client code to remain exactly as it is (continue to
>> import from its original package) but the behavior is enhanced
>>  once this package is imported on startup.
>
> /Something/ has to adjust the pre and post conditions -- if not the
> client code, then what?

pre and post conditions are just one possible use of this.

Going back to my smtplib.SMTP.sendmail example.
No changes in bulk of client code.
Single patch module imported in main.


client.py (no changes)

     from smtplib import SMTP
     def send_email():
         SMTP.sendmail(...)

patch.py (new module)

     from smtplib import SMTP
     from prepost import prepostcall
     SMTP.sendmail = prepostcall(SMTP.sendmail)
     def my_other_func():
         pass
     SMTP.sendmail.before.insert(my_other_function)

main.py (single line modification)

     import patch # new code
     import client
     client.send_email()








More information about the Python-ideas mailing list