It is possible to write a decorator that sets the __doc__ property:
def doc(docstr):
def deco(f):
f.__doc__ = docstr
return f
return deco
This will work for functions and old-style classes, but not for new-style classes, since the __doc__ attribute of new-style classes is read-only (unless you use metaclasses, adding even more complexity for very little gain).
I'd recommend against using such a decorator anyway. Just stick to the usual conventions. That way, fellow developers will immediately understand what's a docstring, tools parsing the source code for docstrings will work correctly etc.
Edit: To also cover new-style classes, you could use this implementation:
def doc(docstr):
def deco(f):
if isinstance(f, type):
d = vars(f).copy()
d["__doc__"] = docstr
return type(f.__name__, f.__bases__, d)
f.__doc__ = docstr
return f
return deco
Since __doc__ is read-only, we need to create a new type object with the desired documentation. For simplicity, I don't consider custom metaclasses (if you want to, replace type by f.__class__ in the above code).
Again, I don't recommend using this decorator.