6 comments

[ 1.2 ms ] story [ 25.7 ms ] thread
14 years old and doesn't work with python 3 though.

From glancing the code I didn't fully get how they manage to hook into a function from the "outside"?

EDIT: someone has ported it to 3: https://github.com/sshirokov/pydvice/pull/1

Ah, it's allowed to mutate a function's `__code__` attribute:

https://github.com/sshirokov/pydvice/blob/abfa0c5f1dc55780a8...

Why is this needed for this library?
Because the library can "advice" an existing function without decorating that function.

    # Write a function
    >>> def there():
    ...   print "there",
    ...
    >>> there()
    there
    
    # Write a peice of 'before advice' to run before the function
    #   New advice is activated by default
    >>> @pydvice.before(there)
    ... def hello():
    ...   print "Hello",
    ...
    
    # Note that active advice runs automatically on any function call
    >>> there()
    Hello there