On Fri, Mar 12, 2021 at 10:41 AM Matt Williams <[email protected]> wrote:
>
> Hi,
>
> It's becoming more popular in Python to have interfaces which are built
> around method chaining as a way of applying operations to data. For example
> in pandas is moving more towards a default model where methods do not change
> the object in-place but instead return an altered version (or at least an
> altered view) of it.
>
> The major downside to method chaining is that it ends up creating long lines
> of code which can become hard to read. The two main solutions to this are 1)
> break down the chain and give the steps their own variable names and 2) split
> it over multiple lines using `\` as a line continuation.
>
> e.g.:
>
> y = x.rstrip("\n").split(":")[0].lower()
>
> would become
>
> y = x.rstrip("\n") \
> .split(":")[0] \
> .lower()
>
> I find the continuation character visually distracting, easy to forget and
> does not allow for line-by-line commenting (causing `SyntaxError: unexpected
> character after line continuation character`):
>
> y = x.rstrip("\n") \
> .split(":")[0] \ # grab the key name
> .lower()
>
> My idea is to alter the syntax of Python to allow doing:
>
> y = x.rstrip("\n")
> .split(":")[0]
> .lower()
>
> i.e., not requiring an explicit line continuation character in the case where
> the next line starts with a period.
>
> I've had a search through the archives and I couldn't see this discussion
> before. Feel free to point me to it if I've missed anything.
Why isn't the implicit line continuation inside parentheses sufficient?
>From my 3.9 REPL:
>>> x
'TEST:TESTING:TEST3\n'
>>> y = x.rstrip("\n").split(":")[0].lower()
>>> y
'test'
>>> z = (x.rstrip("\n")
... .split(":")[0]
... .lower()
... )
>>> z
'test'
>>>
This already works today, and every style guide with an opinion that
I'm aware of prefers this implicit line continuation over the explicit
backslash you demonstrate anyway.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/JFOEB2RY4CWTO4ZM3HTB574DROZRWPTK/
Code of Conduct: http://python.org/psf/codeofconduct/