Skip to content

Instantly share code, notes, and snippets.

@ssebastianj
Last active February 5, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssebastianj/90ba4abba1fe31902a0e954e3c93c774 to your computer and use it in GitHub Desktop.
Save ssebastianj/90ba4abba1fe31902a0e954e3c93c774 to your computer and use it in GitHub Desktop.
import types
def attach_docstring(value, docstring: str):
class_name = type(value).__name__
class_bases = (type(value),)
build_body = lambda ns: ns.update({"__doc__": docstring})
return types.new_class(class_name, class_bases, exec_body=build_body)(value)
if __name__ == "__main__":
delorean_speed = attach_docstring(88, "when this baby hits 88 miles per hour... you're gonna see some serious shit")
assert isinstance(delorean_speed, type(88)), f"Expected an int. Got {type(delorean_speed)!r}"
assert delorean_speed / 2 == 44
assert delorean_speed.__doc__ == "when this baby hits 88 miles per hour... you're gonna see some serious shit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment