Line continuation and comments
Roel Schroeven
roel at roelschroeven.net
Fri Feb 24 16:32:40 EST 2023
Mark Bourne schreef op 24/02/2023 om 22:04:
> Personally, I don't particularly like the way you have to put multiline
> strings on the far left (rather than aligned with the rest of the scope)
> to avoid getting spaces at the beginning of each line. I find it makes
> it more difficult to see where the scope of the class/method/etc.
> actually ends, especially if there are multiple such strings. It's not
> too bad for strings defined at the module level (outer scope) though,
> and of course for docstrings the extra spaces at the beginning of each
> line don't matter.
A way around this is using textwrap.dedent()
(https://docs.python.org/3.10/library/textwrap.html?highlight=dedent#textwrap.dedent).
Example from the documentation:
def test():
# end first line with \ to avoid the empty line!
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
print(repr(dedent(s))) # prints 'hello\n world\n'
--
"Met een spitsvondig citaat bewijs je niets."
-- Voltaire
More information about the Python-list
mailing list