Skip to content

Instantly share code, notes, and snippets.

@LibbyHeeren
Created July 31, 2025 21:47
Show Gist options
  • Select an option

  • Save LibbyHeeren/44c4d9420a7171dae035d3131122c6de to your computer and use it in GitHub Desktop.

Select an option

Save LibbyHeeren/44c4d9420a7171dae035d3131122c6de to your computer and use it in GitHub Desktop.
snippet of python code demonstrating a decorator to let functions inherit docstrings from other functions
def copy_doc(from_func):
"""
A decorator that copies the docstring from another function
for sharing documentation across similar functions.
"""
def decorator(to_func):
to_func.__doc__ = from_func.__doc__
return to_func
return decorator
def base_function(a, b):
"""
Base function that demonstrates a shared docstring.
Parameters
----------
a : int
The first number.
b : int
The second number.
Returns
-------
int
The result of a + b.
"""
return a + b
@copy_doc(base_function)
def derived_function(a, b):
return a + b # Different function name, same functionality, same docstring
# See? Same docstring for both funcs
print("Docstring for base_function:\n")
print(base_function.__doc__)
print("\nDocstring for derived_function (inherited doc string):\n")
print(derived_function.__doc__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment