[Tutor] Writing decorators?

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jul 20 13:09:42 EDT 2016

On 20/07/16 14:30, Michael Welle wrote:

> Now it gets interesting ;). Can you give me a hint on how to modify the
> code of the function in a decorator or even give a small example,
> please? Would I take the route with the bytecode attribute __code__
> (IIRC)? Or use the inspect module? 

Steven changed the function object by modifying the __doc__
attribute. It is probably possible to modify the __code__ too
but I'd strongly recommend that you don't. It's very likely to
result in something so "clever"/complex that it will never
be maintainable, and maintainability beats nearly everything
else in programming priorities.

If you cannot do what you want by wrapping the original
function you should probably just rewrite it to do what
you want. Or, if you own the code, refactor it into chunks
and combine the chunks into a new high level function.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list