Feedback

Chat Icon

Building with GitHub Copilot

From Autocomplete to Autonomous Agents

Smart Inline Completions: Use Cases, Examples, and Exercises
34%

Ask Your Code Questions

You can use Copilot to understand a code snippet by asking questions about it. Remember that the models powering Copilot are LLMs, so they understand human language and can answer questions based on a given context. The context is the code snippet you provide, and the question is the prompt you task Copilot with.

Let's say you have the following code snippet:

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_borrowed = False

class Patron:
    def __init__(self, name):
        self.name = name
        self.books = []

    def borrow(self, book):
        if not book.is_borrowed:
            self.books.append(book)
            book.is_borrowed = True
            return f"{book.title} has been borrowed."
        return "Book is currently unavailable."

class Library:
    def __init__(self):
        self.inventory = []

    def add_book(self, title, author):
        self.inventory.append(Book(title, author))

    def get_book_info(self, title):
        for book in self.inventory:
            if book.title == title:
                return f"{title} by {book.author}, {'Available' if not book.is_borrowed else 'Borrowed'}"
        return "Book not found."


# Usage
lib = Library()
lib.add_book("Animal Farm", "George Orwell")
lib.add_book("Brave New World", "Aldous Huxley")

patron = Patron("Alice")
print(patron.borrow(lib.inventory[0]))  # Attempt to borrow Animal Farm
print(lib.get_book_info("Animal Farm"))

At the end of the code, you can add your question and wait for Copilot to generate the answer. For example, you can ask the following:

# What does the Book class represent and what are its attributes?
# [Wait for the suggestion]

A better approach would be to add an indication that you're asking a question. This can be done by adding Q: before the question and A: before the answer.

Building with GitHub Copilot

From Autocomplete to Autonomous Agents

Enroll now to unlock all content and receive all future updates for free.