Comments vs DocStrings
Feature
Comments
Docstrings
When Should I use a Comment or DocString?
def create_historic_character(name, era):
"""Create a historical character with a name and era.
Args:
name (str): The character's name.
era (str): The historical era.
Returns:
str: A formatted description of the character.
"""
return f"{name} lived during the {era}."
# Example usage
print(create_historic_character("Leonardo da Vinci", "Renaissance"))Last updated
Comments
Structure: Comments can be single-line (// in C++, # in Python) or multi-line (/* ... */ in C++) depending on the programming language. They are typically placed within the code block they are explaining.
Use: Comments are used to explain specific lines of code, algorithms, or logic choices made by the programmer. They can also be used for temporary notes or reminders to be removed later such as #TODO