On 09/01/2016 02:04 PM, Paul Moore wrote:
Thanks. That's a nice example of how the proposal might help. But you
could of course have written your original code as

    def update(names, value):
        (dbsession.query(Table1)
            .filter(Table1.name.in_(names))
            .update({'value': value})
        (dbsession.query(Table2)
            .filter(Table2.name.in_(names))
            .update({'value': value}))

That's not going to completely alleviate the problem, but it does make
the intent clearer. And it's possible that you could propose a style
rule that a dedent in a bracketed expression is not allowed - that
might well be something that flake8 could add, and then you'd get a
much clearer error message (but only if you ran flake8 - if you just
saw a syntax error from Python, you'd probably be just as likely to
"fix" it as you said above, without even trying to run flake8). Also,
of course, most text editors would highlight matching parentheses -
which makes it much easier to spot the "correct" place for the missing
parenthesis.

Good points.  That style does look clearer than my example.

One other thing, I'm not at all keen on using "..." for the syntax -
it's almost completely invisible when I read this in gmail, and as
others have pointed out, it already has a meaning, as Ellipsis. But I
don't have a better suggestion to offer, I'm afraid.

Now that I remember that "..." is a literal value in Python 3 (surprise!), I've been thinking about alternatives. I wonder if a double backslash would work. Double backslash at the end of a line is currently a syntax error. Let's see how it looks:

    def update(names, value):
        dbsession.query(Table1) \\
            .filter(Table1.name.in_(names))
            .update({'value': value})
        dbsession.query(Table2) \\
            .filter(Table2.name.in_(names))
            .update({'value': value})

That looks OK to me right now. Double backslash seems to convey the idea that it's not only a line continuation, but a line continuation with special properties.

I would never write it this way, BTW:

    def update(names, value):
        dbsession.query(Table1) \
            .filter(Table1.name.in_(names)) \
            .update({'value': value})
        dbsession.query(Table2) \
            .filter(Table2.name.in_(names)) \
            .update({'value': value})

I never seem to be able to sprinkle those single backslashes in the right places consistently. I forget one, or I leave one in after rearranging lines, leading to mayhem.

Also, it's very definitely "yet another way to
write expressions across multiple lines". But the indented expression
format is pretty readable for cases when you *do* have a long
expression and no convenient way to bracket it.

Well, you could say the same about triple quoted strings. Sure there are other ways to write strings longer than a single line, but triple quoted strings are clearly worth the extra parser complexity.

Shane

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to