Doctests and decorated methods don't get along
John Posner
jjposner at optimum.net
Sat Feb 6 14:00:28 EST 2010
On 2/6/2010 6:48 AM, Steven D'Aprano wrote:
> class MyStaticMethod(object):
> """Emulate built-in staticmethod descriptor."""
> def __init__(self, f):
> self.f = f
> def __get__(self, obj, objtype=None):
> return self.f
How about using a function, instead of a class, to implement the decorator:
import functools
def MyStaticMethod(f):
@functools.wraps(f)
def _wrapper(*args, **kwargs):
return f(*args, **kwargs)
return _wrapper
For me, this produces the hoped-for doctest failure in
__main__.Test.method3.
I tried making the class-based implementation work by using
functools.wraps(), but I couldn't manage it. Also, doesn't the decorator
protocol want the class to implement a __call__() method, not a
__get__() method? Or am I getting hung up (not the first time) at the
nexus of the descriptor and decorator protocols?
-John
More information about the Python-list
mailing list