Decorator cllass hides docstring from doctest?

Felipe Almeida Lessa felipe.lessa at gmail.com
Thu Sep 21 09:17:46 EDT 2006


2006/9/21, Berthold Höllmann <berthold at despammed.com>:
> Saving the following code to a file and running the code through
> python does not give the expected error. disableling the "@decor" line
> leads to the expected error message. Is this a bug or an overseen
> feature?

Try the new_decor class described below:

>>> class decor(object):
...     def __init__(self, f):
...             self.f = f
...     def __call__(self, *args, **kw):
...             return self.f(*args, **kw)
...
>>> class new_decor(object):
...     def __init__(self, f):
...             self.f = f
...             self.__doc__ = f.__doc__
...             self.__name__ = f.__name__
...     def __call__(self, *args, **kw):
...             return self.f(*args, **kw)
...
>>> def f(a, b):
...     '''
...     >>> f(1, 2)
...     False
...     >>> f(2, 2)
...     False
...     '''
...     return a == b
...
>>> f.__doc__
'\n\t>>> f(1, 2)\n\tFalse\n\t>>> f(2, 2)\n\tFalse\n\t'
>>> decor(f).__doc__ == f.__doc__
False
>>> new_decor(f).__doc__ == f.__doc__
True


-- 
Felipe.


More information about the Python-list mailing list