Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a longer docstring
#1
a function i am creating needs a longer docstring. it's not huge, but it is longer than 80 characters. to stay all on one line, it will need to be around 120 characters long. that or it needs to be split to two lines in a way that still leaves just one line in the string.

i am wondering what is the common way to do this. using triple quotes will cause two lines in the string. or should this be done with just one quote?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I've been experimenting and you can use parentheses and implicit string concatenation
>>> def func():
...     ("This is the beginning of a very long docstring which must stand on one line"
...     " now the rest of this very long docstring hope it will be allright.")
...     print('Hello from func()')
... 
>>> func()
Hello from func()
>>> func.__doc__
'This is the beginning of a very long docstring which must stand on one line now the rest of this very long docstring hope it will be allright.'
The uncommon part is that usually docstrings have a reasonably sized first line and may have other lines.
Reply
#3
that looks like a nice way to code all docstrings, just to be consistent between functions. when both parenthesis are on the same line with just one literal string, they have no real effect.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
One can also use the backslash as a line continuation character
>>> def func():
...     "The first part of the docstring,"\
...     " now the rest of it,"\
...     " and some more."
...     pass
... 
>>> func()
>>> func.__doc__
'The first part of the docstring, now the rest of it, and some more.'
An even wilder option
>>> def func():
...     "spam \
... ham \
... eggs"
...     print('Hi')
... 
>>> func()
Hi
>>> func.__doc__
'spam ham eggs'
>>> 
Reply
#5
I prefer to stick to conventions. PEP8 for line length and PEP257 Docstring Conventions for docstring. If you don't follow the first then you will face problems with implementing the second.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Aug-25-2022, 01:40 PM)perfringo Wrote: I prefer to stick to conventions. PEP8
This is probably the most reasonable thing to do, however when I started programming Python more than 25 years ago, there was no PEP8, we lived in Python's paradise, therefore I feel no guilt at all in breaking all these rules that were invented later. There was really a huge sense of freedom in the small Python community at that time.

Also note that subjecting every Python software to follow PEP8 is an abuse of PEP8. Indeed, it was only intended as a style guide for developers of the standard library. GvR was a benevolent dictator but not a megalomaniac.
Reply
#7
Docstrings and their conventions are important when you write code for others to read/use. Like PEP257 mentions:

Quote:But some software (such as the Docutils docstring processing system PEP 256, PEP 258) will be aware of the conventions, so following them will get you the best results.

/.../

Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line.

For personal project you do whatever pleases you Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
PEP8 Wrote:Many projects have their own coding style guidelines. In the event of any conflicts, such project-specific guides take precedence for that project.
This means that any project can do whatever pleases them.
PEP257 Wrote:“A universal convention supplies all of maintainability, clarity, consistency, and a foundation for good programming habits too. What it doesn’t do is insist that you follow it against your will. That’s Python!”
—Tim Peters on comp.lang.python, 2001-06-16
For me, Python still rhymes with freedom.
Reply
#9
(Aug-25-2022, 01:40 PM)perfringo Wrote: I prefer to stick to conventions.
what is the convention for a case of staying within 71 columns per line in the coding and having up to 120 characters of text for a docstring?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner: Code not work when longer list raiviscoding 2 821 May-19-2023, 11:19 AM
Last Post: deanhystad
  IDLE editing window no longer works chris1 2 2,210 Feb-06-2021, 07:59 AM
Last Post: chris1
  Code no longer working yk303 14 10,175 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,540 Dec-14-2020, 07:34 PM
Last Post: buran
  calling a method for a docstring Skaperen 8 3,013 Jan-27-2020, 01:16 AM
Last Post: Skaperen
  get docstring to work with another block of text at the top Skaperen 4 2,449 Dec-28-2019, 10:37 PM
Last Post: Skaperen
  My code is taking longer time to give result rajeshwin 4 3,304 Feb-20-2019, 08:18 PM
Last Post: ichabod801
  Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 3,383 Feb-02-2019, 10:15 AM
Last Post: Larz60+
  ptpython docstring metulburr 4 4,478 Nov-17-2017, 01:36 AM
Last Post: metulburr
  long lines, longer than 72 or 79 Skaperen 1 3,133 Mar-08-2017, 07:56 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020