[Python-ideas] A directive for indentation type, stricter indentation parsing.

Mikhail V mikhailwas at gmail.com
Tue Mar 26 08:44:59 EDT 2019


On Tue, Mar 26, 2019 at 2:02 PM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Tue, Mar 26, 2019 at 9:49 PM Mikhail V <mikhailwas at gmail.com> wrote:
> > Procedural removal is not cool, because it does not know the parent indentation
> > [...]
> > E.g. this:
> >     s = """
> >                 Hello
> >                 world"""
> >
> > Here I do not want to remove all indents but only as in above - only 1
> > level inside string
> > and 1 from parent line.
>
> This is true if you put your closing quotes on the same line as the
> last line of text, because then there's no information available. If,
> instead, you put the final triple-quote delimiter on its own line, you
> then have the indentation preserved, and can remove just that much
> indentation from each line. So, yes, you CAN unindent automatically.
>
> Point of note: PEP 257 recommends this for docstrings.
>
> https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
>
> ChrisA


Yes you are right. If closing triple quote is on a separate line , I
can use the last line
contents and extract it from previous lines. I should have remembered that
trick and it is cool actually. So technically I can do it.
So my point was that it would be cool to have a dedicated statement for this and
so it works on parser level plus would look cleaner (no closing
quotes), and no need to care
about  escaping of """ sequence if it appears inside the string, but I
think that would be impossible
without the parser knowing the indent method in advance.


More information about the Python-ideas mailing list