[Python-ideas] __before__ and __after__ attributes for functions

Chris Angelico rosuav at gmail.com
Fri Jan 24 08:10:06 CET 2014


On Fri, Jan 24, 2014 at 4:09 PM, Ethan Furman <ethan at stoneleaf.us> 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?

# foo.py:
import blah
blah.quux()

# bar.py:
import blah
blah.quux.__before__.append(......)
import foo


With code like that, modifying/rebinding the 'quux' inside bar.py
won't affect what happens when foo is imported, ergo monkeypatching
the blah module is key.

ChrisA


More information about the Python-ideas mailing list