Skip to content

Instantly share code, notes, and snippets.

@llllllllll
Created May 13, 2020 19:59
Show Gist options
  • Save llllllllll/df9534a46e3c047fbde16915afce3102 to your computer and use it in GitHub Desktop.
Save llllllllll/df9534a46e3c047fbde16915afce3102 to your computer and use it in GitHub Desktop.
from collections import UserString
class ResolvedDocString(UserString):
__class__ = str
class DeferDocString(UserString):
def __init__(self, f):
self._f = f
def __getattribute__(self, attr):
if attr == '__class__':
return str
elif attr == '_f':
return super().__getattribute__(attr)
val = self._f()
del self._f
self.__class__ = ResolvedDocString
self.data = val
return getattr(self, attr)
In [1]: from defer_doc import DeferDocString
In [2]: class C:
...: def m(self):
...: pass
...:
In [3]: C.m.__doc__ = DeferDocString(lambda: 'test')
In [4]: C.m?
Signature: C.m(self)
Docstring: test
File: ~/projects/tmp/<ipython-input-2-871863a5a2d3>
Type: function
In [5]: C.m?
Signature: C.m(self)
Docstring: test
File: ~/projects/tmp/<ipython-input-2-871863a5a2d3>
Type: function
In [6]: C.m.__doc__[0]
Out[6]: 't'
In [7]: C.m.__doc__.upper()
Out[7]: 'TEST'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment