[Chicago] doc strings Was: How could I make this better?

Vern Ceder vceder at gmail.com
Sun Mar 6 15:55:47 CET 2011


On Sun, Mar 6, 2011 at 9:44 AM, Carl Karsten <carl at personnelware.com> wrote:
> 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
>> string...
>
> 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: ' " """

One minor thing to add is that you can also use triple *single*
quotes, although it's not common for some reason, so ''' does exactly
the same thing as """.

Cheers,
Vern

>
> 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:'"""
> 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.
>
>
> --
> Carl K
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>



-- 
Vern Ceder
vceder at gmail.com, vceder at dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW


More information about the Chicago mailing list