[Chicago] doc strings Was: How could I make this better?
carl at personnelware.com
Sun Mar 6 15:44:31 CET 2011
On Sun, Mar 6, 2011 at 4:31 AM, Jon Sudlow <jsudlow at gmail.com> wrote:
> why are printing the string using three quotes? I know three means a doc
Not exactly, and it's good to understand the subtle differences.
Strings can be delimited with the single quote char, double quote char or 3
double quote chars: ' " """
Single and double are interchangeable. 2 exist to make it easy to embed one
of the other: "don't" and 'he said, "The parrot is dead."' and if you
need both you can escape:
>>> print 'he said, "It\'s napping"'
he said, "It's napping"
Neither of these let you span lines:
>>> print 'he said, "It\'s napping
SyntaxError: EOL while scanning string literal
Tripple quotes let you embed either of the quote chars and span lines:
>>> print """double quote:"
... single quote:'"""
on doc strings:
python lets you put an expression on a line without assigning it to
anything. This lets you call functions that do something but don't return a
value you care to save:
>>> print('how do you feel?')
how do you feel?
constants are expressions. I can't give a demo using the command prompt,
because this is where the python shell's features and running code
diverge. shell sends return values to stdout, running code does not.
Doc strings are just a multi line string (a constant) sitting in code.
being at the top of a module/function/class makes it a doc string. There
are helper tools (like the help function) that will look there for a string.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Chicago