Sunday, April 17, 2011

indentation of multiline string

I have a script that uses the cmd Python module. The cmd module uses a triple quoted multiline string as it's help text. Something like this

def x(self, strags = None):
    """class
    help text here
    and some more help text here"""

When running the script, the command 'help x' will print the string. It will, however, print the newlines in front of the last two lines as well. I can overcome this by not indenting these lines, but that'll make my code ugl{y,ier}.

How to overcome this indenting problem? How do the pro Python coders handle this?

From stackoverflow
  • I'd handle it by having consistent indents, like this:

    def x(self, strags = None):
        """
        class
        help text here
        and some more help text here
        """
    

    Sure, it takes two lines more, but it also injects clarity (in my opinion) by making the doc comment stand out quite well.

  • Personally I try to follow PEP 8 which refers the reader to PEP 257 for Docstring Conventions. It has an entire section on multi-line docstrings.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.