doctests and decorators

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Jun 16 22:20:27 EDT 2009


On Tue, 16 Jun 2009 12:04:32 -0700, Scott David Daniels wrote:

> Eric Snow wrote:
>> In general should decorators always hide themselves?  I am guessing
>> not, otherwise this would already be part of their behavior.  Still, is
>> it the common case to camouflage the decorator like this?  If so, I
>> would expect it to be the default behavior of decorators.
> 
> The Python goal is "no magic".  So, if you want the stuff wrapped, you
> do it (as the default traceback shows where the code actually goes). It
> would be far more complicated to display the truth if decorators
> defaulted to modifying the builtins, and you had to do magic to remove
> that part of the decoration.


I'm afraid I can't understand what you're saying. What do you consider 
"magic"? What's a "default traceback"? What do you mean, "display the 
truth"?


> A decorator has _very_ simple semantics,
> while anything that automatically copied attributes would have funny
> semantics indeed for use by funny decorators like:
[...]


functools.wraps() automatically copies attributes:

>>> import functools
>>> def dec(func):
...     @functools.wraps(func)
...     def inner(*args):
...             return func(args) + 1
...     return inner
...
>>> def f(x):
...     return 1
...
>>> f.attr = "Attribute"
>>> f = dec(f)
>>> f(3)
2
>>> f.attr
'Attribute'



-- 
Steven



More information about the Python-list mailing list