Last active
February 5, 2022 13:00
-
-
Save ssebastianj/90ba4abba1fe31902a0e954e3c93c774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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