Python should allow comments AFTER continuation line bar

By stripping the first line and dedenting the rest, do you mean something like:

from textwrap import dedent

def needdir(dirpath, mode=0o777, *, use_makedirs=False, log=None):
    ''' Create the directory `dirpath` if missing.

        Parameters:
        * `dirpath`: the required directory path
        * `mode`: the permissions mode, default `0o777`
        * `log`: log `makedirs` or `mkdir` call
        * `use_makedirs`: optional creation mode, default `False`;
          if true, use `os.makedirs`, otherwise `os.mkdir`
    '''
first, sep, rest = needdir.__doc__.lstrip().partition('\n')
print(sep.join((first, dedent(rest))))

which outputs:

Create the directory `dirpath` if missing.

Parameters:
* `dirpath`: the required directory path
* `mode`: the permissions mode, default `0o777`
* `log`: log `makedirs` or `mkdir` call
* `use_makedirs`: optional creation mode, default `False`;
  if true, use `os.makedirs`, otherwise `os.mkdir`

If so, that’s a bit too much code for me just to define a string literal in a pretty way.

Perhaps dedenting and then left-stripping may be a cleaner alternative:

code = dedent('''
    for i in range(10):
        print(i)
''').lstrip()
1 Like

That’s fairly nice. I use this:

My big use case is after the fact, which is why the example docstring
isn’t garnished up with this stuff - I build module README.md files by
processing the docstrings, and do the fancy stripping during that
process. This leaves me with compact and readable docstrings and
dedented/stripped text in the readme file.

If I’m using this in the code I’d generally like:

 from cs.lex import stripped_dedent as dent
 ............
 class X:
     ''' Purely to get some indent in this example.
     '''
     def use_the_code(self):
         code = dent('''
             for i in range(10):
                 print(i)
         ''')

as required. Looks succinct to me, with no quotes hard against the left
edge (column 1).

I’m a big believer in making anything I do regularly a function so that
code isn’t littered with expressions-I-have-to-decode-to-understand. I
keep a host of handy little lexical things in cs.lex ready to hand,
and I’m sure nearly none of them belong in the stdlib.

I’ll omit my reflex snarky remark here :slight_smile: But I suspect that choice may
have been influenced by things like CP/M having filenames like
‘filename/ext’, which meant that a forward slash might not have been
considered not very available for a path separator.

Yes, UNIX was well around then anyway with slashes.

Or of course it could have just been Microsoft being Microsoft and being
deliberately different and incompatible. This is the same company who
devised their own network connection protocol when TCP already existed
IIRC. Corporately they’ve always been a bit like this, for all that they
have many good people doing good things in good faith.

| Chris Angelico Rosuav
May 2 |

  • | - |
if a == b and c == 1 and d >= 2.7   \  # Here I couldn't use quotations!

I think this might annoy a few people.

And would be an unnecessary restriction, I think. There’s currently no problem with having quote characters in a comment, and I don’t see why these proposed backslash-following comments should be any different.

Trying to make it possible to put comments inside a string literal, on the other hand,would be madness. There are already ways to get the same effect, e.g.

s = ("This is a string" # just in case you didn't notice
     " and here is some more of it.")
1 Like

| Karl Knechtel kknechtel
May 2 |

  • | - |

I would much rather have a syntax where line continuations are marked at the beginning of the continuation.

We should support

from __future__ import fortran_revival

so you can put a continuation mark in column 6.

4 Likes

| dosergio@gmail.com sabre
May 2 |

  • | - |

It is like a client saying: “The chicken is bad in this resturant”. Instead of them look and try to modify the chicken recipe ( my first intent), they say: eat pork and leave the chiken.

No, it’s more like saying “I don’t think this restaurant should promise that its chicken is organically grown, because I don’t care about that”, and the restaurant saying “If you don’t care, why is that a problem?”