Python caveats #1: Multiline comment within a Python dict
Tags: python, tips, beginners
Published: 2019-05-22 12:34:21

---
title: Python caveats #1: Multiline comment within a Python dict
published: true
description: How to put multiline comments in python dict? Should we use triple quotes style or multiline hashes?
tags: python,tips,beginners
canonical_url: https://www.rookieslab.com/posts/python-caveats-multiline-comment-inside-a-dict
cover_image: https://www.rookieslab.com/assets/documentation/python-caveats-1.jpg
---

Let's say we have a Python dict and we were to document the general format of the key-value pairs. For the lack of a better example, let's take the following dict as an example:

```
family = {
    "goku": {
        "name": "Goku",
        "race": "Saiyan",
        "aliases": ["Son Goku", "Kakarot"],
    },
    "chi-chi": {
        "name": "Chi Chi",
        "race": "Human",
        "aliases": ["The Ox-Princess"],
    },
    "gohan": {
        "name": "gohan",
        "race": "Saiyan",
        "aliases": ["Great Saiyaman", "Son Gohan"],
    },
}
```

The general format of the key-value pair is:

```
"username": {
    "name": "",
    "race": "",
    "aliases" [],
}
```

And to add this as comment, we might try this:

```
family = {
    """
    General format of entries is

    "username": {
        "name": "",
        "race": "",
        "aliases" [],
    }

    """
    "goku": {
        "name": "Goku",
        "race": "Saiyan",
        "aliases": ["Son Goku", "Kakarot"],
    },
    "chi-chi": {
        "name": "Chi Chi",
        "race": "Human",
        "aliases": ["The Ox-Princess"],
    },
    "gohan": {
        "name": "gohan",
        "race": "Saiyan",
        "aliases": ["Great Saiyaman", "Son Gohan"],
    },
}
```

Let's check the keys of this dict now.

```
In [15]: family.keys()
Out[15]: 
['\n    General format of entries is\n\n    "username": {\n        "name": "",\n        "race": "",\n        "aliases" [],\n    }\n\n    goku',
 'gohan',
 'chi-chi']
```

Hold up! Why'd that modify the `goku` string? This is because surrounding something with `"""` doesn't necessarily mean as comments in Python. It is just a multiline string. Imagine it as a string literal, which isn't assigned to any variable. **There's no such thing as multiline comments in Python, unlike other programming languages.** The right way to put multiline comments is by using `#`.

You'd recall using `"""` to put comments at the beginning of function/class, also otherwise known as docstrings. Well, what do you know, those docstrings aren't really comments. It is just a string literal and a standard convention<a href="#note2" id="note2ref"><sup>[1]</sup></a>.

So the right way to put a comment in dict would be

```
family = {

    # General format of entries is
    # "username": {
    #    "name": "",
    #    "race": "",
    #    "aliases" [],
    # }

    "goku": {
        "name": "Goku",
        "race": "Saiyan",
        "aliases": ["Son Goku", "Kakarot"],
    },
    "chi-chi": {
        "name": "Chi Chi",
        "race": "Human",
        "aliases": ["The Ox-Princess"],
    },
    "gohan": {
        "name": "gohan",
        "race": "Saiyan",
        "aliases": ["Great Saiyaman", "Son Gohan"],
    },
}
```

#### tl;dr

There's no such thing as a multiline comment in Python. Use `#` at every line to truly create multiline comments.

<a id="note1" href="#note1ref">[1]</a> <a href="https://www.python.org/dev/peps/pep-0257/" target="_blank">PEP 257 -- Docstring Conventions</a>  


Originally posted on [rookieslab.com](https://www.rookieslab.com/posts/python-caveats-multiline-comment-inside-a-dict)