Mojo Comments
A comment is a note in your code for people, not for the computer. Mojo ignores comments completely when it builds your program, so you can write anything you like in them. Good comments explain why the code does something, which the code itself can’t always tell you.
Mojo has two kinds of notes: comments, which start with #, and docstrings, which describe what a function or file is for. Let’s look at both.
Single-line comments
Section titled “Single-line comments”A comment starts with # and runs to the end of the line:
def main(): # Greet the user before anything else print("Welcome!")Output:
Welcome!The comment line doesn’t print anything. Only the print does.
Comments at the end of a line
Section titled “Comments at the end of a line”You can also put a comment after some code, on the same line. Everything from the # onwards is ignored:
def main(): var minutes = 90.0 # length of the film print(minutes / 60) # in hoursOutput:
1.5Leave at least one space before the #; two is common. It keeps the comment from looking glued to the code.
Comments over several lines
Section titled “Comments over several lines”Mojo has no special “block comment” syntax like /* ... */ in C. To write a longer note, start each line with #:
def main(): # This program prints a short menu. # Prices are in euros and already include tax. # Update them every January. print("Coffee: 2.5") print("Tea: 2.0")Output:
Coffee: 2.5Tea: 2.0A # inside text is not a comment
Section titled “A # inside text is not a comment”If the # is inside quotes, it’s part of the text, not the start of a comment:
def main(): print("Order #1042") # this part is a commentOutput:
Order #1042Commenting out code
Section titled “Commenting out code”Comments are handy for switching a line off without deleting it. Put a # in front of it, and Mojo skips it:
def main(): print("Step 1") # print("Step 2") print("Step 3")Output:
Step 1Step 3That’s useful while you’re testing something. Don’t leave lots of commented-out code lying around for good, though: it confuses the next reader, who can’t tell whether it’s still needed.
Docstrings
Section titled “Docstrings”A docstring is a text in triple quotes (""") placed right at the start of a function, a file or a type. Unlike a comment, it’s part of your program: Mojo keeps it, and tools like your editor and the mojo doc command read it to show help.
"""Small helpers for greeting people."""
def greet(name: String): """Prints a friendly greeting.
Args: name: The person to greet. """ print("Hello,", name)
def main(): greet("Ada") greet("Linus")Output:
Hello, AdaHello, LinusHere’s what’s going on:
- The docstring at the very top describes the whole file.
- The docstring inside
greetdescribes the function. The first line is a short sentence saying what it does. After a blank line, theArgs:section explains each argument. - In VS Code with the Mojo extension, hovering over
greetshows this text.
The official docstring reference has the full format, including sections like Returns: and Raises:. You’ll write more docstrings once you start writing your own functions.
Writing comments that help
Section titled “Writing comments that help”A comment that repeats the code doesn’t help anyone:
def main(): var count = 0 count = count + 1 # add 1 to count print(count)Output:
1# add 1 to count says exactly what the code already says. A useful comment tells the reader something they can’t see:
def main(): var retries = 3 # the server sometimes drops the first request print("Retries:", retries)Output:
Retries: 3Some simple habits:
- Explain why, not what. The code already shows what.
- Keep comments up to date. A wrong comment is worse than no comment.
- If you need a long comment to explain a confusing line, try making the code clearer first (better names often do the job).
# TODO:is a common way to mark something you still need to do. Many editors highlight it.
Practice
Section titled “Practice”1. Explain a number
Section titled “1. Explain a number”This program converts a temperature, but nobody knows where 1.8 and 32 come from. Add a comment on the line above the calculation that explains it. The output should stay the same.
def main(): var celsius = 25.0 print(celsius * 1.8 + 32)Output:
77.0Show solution
def main(): var celsius = 25.0 # Celsius to Fahrenheit: multiply by 1.8 (9/5), then add 32 print(celsius * 1.8 + 32)Output:
77.0The comment doesn’t change what the program does, but now the next reader knows what the formula is for.
2. Add a docstring
Section titled “2. Add a docstring”Write a function print_banner that prints === Mojo ===. Give it a one-line docstring that says what it does, and call it from main.
Hint: the docstring goes on the first line inside the function, in triple quotes.
Show solution
def print_banner(): """Prints the program's title banner.""" print("=== Mojo ===")
def main(): print_banner()Output:
=== Mojo ===The docstring is the first thing in the function’s body. Mojo keeps it as documentation, but it doesn’t print anything by itself.
- A comment starts with
#and runs to the end of the line. Mojo ignores it. - For a longer note, start every line with
#; there’s no block comment syntax. - A
#inside quotes is just text. - Put
#in front of a line to comment it out while testing. - A docstring (
"""...""") at the start of a function or file describes what it’s for, and tools can show it. - Good comments explain why, and stay up to date.
Next up: output, everything you can do with print.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Comments in the lexical structure reference: the official rules for comments.
- Docstring reference: the full docstring format, with all the sections you can use.
mojo doc: the command that turns docstrings into documentation.mojo format: the code formatter, which also tidies up spacing around comments.