From d9e6bc6acc25ead20d0fbb56601dd45ab83c7fc5 Mon Sep 17 00:00:00 2001 From: SirDickinson <44280186+SirDickinson@users.noreply.github.com> Date: Fri, 9 Nov 2018 06:59:13 -0600 Subject: [PATCH] Adding multiline comments using triple apostrophes (#21296) --- guide/english/python/commenting-code/index.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/guide/english/python/commenting-code/index.md b/guide/english/python/commenting-code/index.md index 0bae0874e0b..6cd90b5875b 100644 --- a/guide/english/python/commenting-code/index.md +++ b/guide/english/python/commenting-code/index.md @@ -15,9 +15,21 @@ Python does not include a formal way to write multiline comments. Each line of a # This is the first line of a multiline comment. # This is the second line. ``` -Another type of comment is the **docstring**, documented in `PEP 257`. Docstrings are a specific type of comment that becomes the `__doc__` attribute. +Alternatively you could use `'''` to write a a comment that spans multiple lines to avoid having to use the `#`. +For example: +```python + ''' + This is a multiline comment, + everything inside the three + apostrophes will be regarded + by Python as a comment and + ignored when running a program + ''' +``` -For a string literal to be a docstring, it must start and end with `\"\"\"` and be the first statement of the module, function, class, or method definition it is documenting: +Another type of comment is the **docstring**, documented in [`PEP 257`](https://www.python.org/dev/peps/pep-0257/). Docstrings are a specific type of comment that becomes the `__doc__` attribute. + +For a string literal to be a docstring, it must start and end with `"""` and be the first statement of the module, function, class, or method definition it is documenting: ```python class SomeClass(): @@ -32,4 +44,4 @@ For a string literal to be a docstring, it must start and end with `\"\"\"` and pass ``` -String literals that start and end with `"""` that are not docstrings (not the first statement), can be used for multiline strings. They will not become `__doc__` attributes. If they are not assigned to a variable, they will not generate bytecode. There is some discussion about using them as multiline comments found here. +String literals that start and end with `"""` that are not docstrings (not the first statement), can be used for multiline strings. They will not become `__doc__` attributes. If they are not assigned to a variable, they will not generate bytecode. There is some discussion about using them as multiline comments found [Multiline Comments in Python - Stack Overflow](http://stackoverflow.com/questions/7696924/multiline-comments-in-python).