-2

I have been using ''' for block comments in yaml. Like:

'''
This
is 
a
comment
'''

I have noticed that this approach isn't one of the answers to the How do you block comment in yaml question. Is there a reason why not to do this (other than terrible multiline string formating glitches in VIM)? Does it get loaded into memory or something else that could be problematic?

Anthon
  • 69,918
  • 32
  • 186
  • 246

2 Answers2

1

YAML comments are started with # separated from other tokens with whitespace and terminate at the end of line

If you do:

'''
This
is
a
comment
'''

You specify a scalar node, that starts and ends with one (1) single quote. That is because in single quoted style scalar nodes, you can insert a single quote by escaping it with a single quote. Since YAML does line unwrapping the above loads as the string ' This is a comment ' (the string including the quotes).

However if you insert that as comment after a scalar node like 42 as in:

answer: 42 '''
  This
  is
  a
  comment
  '''

You still have valid YAML, but this will load e.g. in Python as a dict with a key answer and an associated value of 42 ''' This is a comment '''. A string, which would probably give you some error if you expected the integer value 42.

Anthon
  • 69,918
  • 32
  • 186
  • 246
0

Based on the spec, use # only: http://yaml.org/spec/1.2/spec.html#comment/

As to why? Short of 'Because they said so' I would guess that some of the readability of YAML is lost with multiline comments.

You're use of ''' is the standard for Python docstrings.

d g
  • 1,594
  • 13
  • 13
  • [PEP 257](https://www.python.org/dev/peps/pep-0257/) explicity states: `For consistency, always use """triple double quotes""" around docstrings`, so the use of `'''` is not the standard for Python docstrings. – Anthon May 14 '18 at 06:18