Member-only story
Streamline Your Documentation with GPT-3
Generating Python docstrings using artificial intelligence
GPT-3, the latest language model developed by OpenAI, has the ability to generate human-like text, making it a powerful tool for a variety of natural language processing tasks.
The model was also trained on programming languages. One use case for this is the generation of docstrings for Python functions.
What are docstrings?
A docstring is a string that appears as the first statement in a Python function. It provides a brief description of the function and its inputs and outputs, making it easier for other developers to understand and use the code. Writing clear and informative docstrings can be time-consuming and tedious, especially for large projects with many functions.
# A docstring example
def square(n):
"""Takes in a number n and returns the square of n"""
return n**2
GPT-3 can be used to generate docstrings by providing it with the source code of the Python function inside of a helpful prompt.
Example: Calculating the average value of a list of numbers
Suppose you wrote a function for calculating the average value of a list of numbers…