473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Multi-line docstrings

The Python docs recommend the use of triple-quoted string literals for
docstrings, e.g.

def Meet(Alice, Bob) :
"""arranges a meeting between Alice and Bob.
Returns a reference to the meeting booking object."""
...
#end Meet

However, these tend to get messed up by indentation whitespace, which gets
spuriously included as part of the string.

Another possibility is to use implicit concatenation of string literals,
e.g.

def Meet(Alice, Bob) :
"arranges a meeting between Alice and Bob." \
" Returns a reference to the meeting booking object."
...
#end Meet

This includes no spurious whitespace, or even any newlines; if you want
these, you must put them explicitly in the string:

def Meet(Alice, Bob) :
"arranges a meeting between Alice and Bob.\n" \
"Returns a reference to the meeting booking object."
...
#end Meet

Dec 23 '06 #1
4 2309
Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
The Python docs recommend the use of triple-quoted string literals for
docstrings, e.g.

def Meet(Alice, Bob) :
"""arranges a meeting between Alice and Bob.
Returns a reference to the meeting booking object."""
...
#end Meet
However, these tend to get messed up by indentation whitespace, which
gets spuriously included as part of the string.
Not spuriously included: included by design, but sometimes annoying. If you
are just printing __doc__ in interactive mode then live with the extra
whitespace. If you are printing out lots of docstrings as part of some
documentation tool then use textwrap.dedent to remove the common leading
spaces.
>
Another possibility is to use implicit concatenation of string
literals, e.g.
<snip>
This includes no spurious whitespace, or even any newlines; if you
want these, you must put them explicitly in the string:
<snip>

.... which is why the triple-quoted format is recommended. BTW, you missed
this way of doing it which ensures that all lines, even the first one have
the same indentation (and therefore textwrap.dedent will strip it quite
nicely):

def Meet(Alice, Bob) :
'''\
arranges a meeting between Alice and Bob.
Returns a reference to the meeting booking object.
'''

and if you want to do string concatenation you can always get rid of those
pesky backslashes:

def Meet(Alice, Bob) :
("arranges a meeting between Alice and Bob.\n"
"Returns a reference to the meeting booking object.")
Dec 23 '06 #2
Duncan Booth:
Not spuriously included: included by design, but sometimes annoying.
Then it's a design decision I don't understand...

Bye,
bearophile

Dec 23 '06 #3
be************@lycos.com wrote:
Duncan Booth:
>Not spuriously included: included by design, but sometimes annoying.

Then it's a design decision I don't understand...
I think the decision is that the doc string should reflect exactly what you
entered in the string. i.e. the system shouldn't tamper with it behind the
scenes.
Dec 23 '06 #4
In message <Xn*************************@127.0.0.1>, Duncan Booth wrote:
Lawrence D'Oliveiro <ld*@geek-central.gen.new_zealandwrote:
>The Python docs recommend the use of triple-quoted string literals for
docstrings, e.g.

def Meet(Alice, Bob) :
"""arranges a meeting between Alice and Bob.
Returns a reference to the meeting booking object."""
...
#end Meet
However, these tend to get messed up by indentation whitespace, which
gets spuriously included as part of the string.

Not spuriously included: included by design, but sometimes annoying.
The problem is that the treatment of indentation whitespace in triple-quoted
strings is at odds with its use for syntax purposes elsewhere in the
language.

Instead of preserving all included whitespace, a better rule for
triple-quoted strings might be to strip whitespace up to the current
indentation level from each continuation line. That is, each line _must_
begin with at least that amount of whitespace, otherwise it's an error; any
extra whitespace is preserved. E.g.

a = """two lines
of text"""

being equivalent to

a = "two lines\n of text"

Or, a simpler rule might be to strip _all_ whitespace at the start of each
continuation line, regardless of indentation level. So the triple-quoted
example above becomes equivalent to

a = "two lines\nof text"

If you _want_ to include some whitespace at the start of a continuation
line, simply precede it by something that isn't literal whitespace:

a = """two lines
\x20 of text"""

becomes equivalent to

a = "two lines\n of text"

(It might be nice if "\ " was recognized as equivalent to "\x20" for this
purpose.)
Dec 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.