News for hackers. Stuff that matters.
Re:What's Coming in Python 3.8 by kbd on Wednesday July 17, 2019 @05:57PM (#20464463)
Despite controversy, walrus operator is going to be like f-strings. Before: "Why do we need another way to..." After: "Hey this is great".

People are wtf-ing a bit about the positional-only parameters, but I view that as just a consistency change. It's a way to write in pure Python something that was previously only possible to say using the C api.

Re:What's Coming in Python 3.8 by stefco_ on Wednesday July 17, 2019 @06:44PM (#20464794)
f-strings are the first truly-pretty way to do string formatting in python, and the best thing is that they avoid all of the shortcomings of other interpolation syntaxes I've worked with. It's one of those magical features that just lets you do exactly what you want without putting any thought at all into it.

Digression on the old way's shortcomings: Probably the most annoying thing about the old "format" syntax was for writing error messages with parameters dynamically formatted in. I've written ugly string literals for verbose, helpful error messages with the old syntax, and it was truly awful. The long length of calls to "format" is what screws up your indentation, which then screws up the literals (or forces you to spread them over 3x as many lines as you would otherwise). It was so bad that the format operator was more readable. If `str.dedent` was a thing it would be less annoying thanks to multi-line strings, but even that is just a kludge. A big part of the issue is whitespace/string concatenation, which, I know, can be fixed with an autoformatter [0]. Autoformatters are great for munging literals (and diff reduction/style enforcement), sure, but if you have to mung literals tens of times in a reasonably-written module, there's something very wrong with the feature that's forcing that behavior. So, again: f-strings have saved me a ton of tedium.

[0] https://github.com/python/black

Re:What's Coming in Python 3.8 by lewiscollard on Thursday July 18, 2019 @07:32AM (#20468688)
For me, the hugs thing about f-strings was that invalid string format characters become a compile time error (SyntaxError).

  print('I do not get executed :)')
  f'{!}'


  File "stefco.py", line 2
    f'{!}'
    ^
  SyntaxError: f-string: empty expression not allowed
This has the pleasing characteristic of eliminating an entire class of bug. :)
Re:What's Coming in Python 3.8 by bulatb on Wednesday July 17, 2019 @06:51PM (#20464834)
> If `str.dedent` was a thing

Have you looked at textwrap.dedent?

Re:What's Coming in Python 3.8 by stefco_ on Wednesday July 17, 2019 @07:19PM (#20465027)
Yes! `textwrap.dedent` is great. On further reflection `wrap` is actually more useful for this kludge (see below). But my point is that that's a whole import for a kludge. Compare the f-string ideal (by my standards):

  raise ValueError("File exists, not uploading: "
                   f"{filename} -> {bucket}, {key}")
...which is short enough that it's readable, and it's clear where exactly each variable is going. It's the single obvious solution, so much so that I don't spend a second thinking about it (very Pythonic!). Compare it to using `str.format` with the same continued indentation:

  raise ValueError(("File exists, not uploading: {filename} -> "
                    "{bucket}, {key}").format(filename=filename,
                                              bucket=bucket,
                                              key=key))
Even this minimal example looks terrible! Remember that a lot of exceptions are raised within multiply-nested blocks, and then format pushes things farther to the right (while also ruining your automated string-literal concatenation, hence the extra parentheses), leaving very little room for the format arguments. You can use a more self-consistent and readable indentation strategy:

  raise ValueError(
      (
          "File exists, not uploading: {filename} -> "
          "{bucket}, {key}"
      ).format(filename, bucket, key)
  )
This is unquestionably more pleasant to read than the former, but it's 3 times longer than the simple f-string solution, and I would argue it is not any more readable than the f-string for this simple example. My point with having a `str.wrap` builtin is that at least you could use the docstring convention of terminating multi-line strings on a newline, which would get rid of the string concatenation issues while leaving you a consistent (albeit diminished by the "wrap" call) amount of rightward room for the `format` args:

  raise ValueError("""File exists, not uploading: {filename} ->
                   {bucket}, {key}
                   """.dedent().format(filename=filename,
                                       bucket=bucket, key=key))
Maybe a little bit better than the first one, especially if you're writing a longer docstring and don't want to think about string concatenation. But still a kludge. You can use positional formatting to shorten things up, but the fundamental weakness of `str.format` remains.
Re:What's Coming in Python 3.8 by bpchaps on Wednesday July 17, 2019 @09:23PM (#20465775)
Here's a clean way to do that:

  str_fmt = "File exists, not uploading: {filename} -> {bucket}, {key}"
  fmt_vals = dict(filename=filename, bucket=bucket, key=key)
  raise ValueError(str_fmt.dedent().format(**fmt_vals))
Re:What's Coming in Python 3.8 by stefco_ on Wednesday July 17, 2019 @09:37PM (#20465881)
This is somewhat cleaner, and I also use this idiom when things get ugly with the inline formatting shown above. But my point is that none of these are very elegant for an extremely common use case. Throw this block in the middle of some complex code with a few try/except and raise statements and it still looks confusing. Having two extra temp variables and statements per error in a function that's just doing control flow and wrapping unsafe code can double your local variable count and number of statements across the whole function. AFAIK, there has been no elegant solution to this common problem until f-strings came around; the only decently clean one is using printf-style format strings with the old-style operator, but outside of terseness I find it less readable.
Re:What's Coming in Python 3.8 by JoBrad on Monday July 29, 2019 @08:59AM (#20554114)
Alternate “clean” way, but sort of hacky.

  raise ValueError(“File exists, not uploading: {filename} -> {bucket}, {key}”.format(**locals()))
Re:What's Coming in Python 3.8 by Dylan16807 on Thursday July 18, 2019 @02:21AM (#20467246)
f-strings are nice, but when the problem is indenting too far, what if you just... didn't do that?

  raise ValueError(("File exists, not uploading: {filename} -> "
      "{bucket}, {key}").format(filename=filename, bucket=bucket, key=key))
Re:What's Coming in Python 3.8 by choppaface on Thursday July 18, 2019 @02:31AM (#20467305)
Was the walrus operator really worth "The PEP 572 mess"? https://lwn.net/Articles/757713/

That post makes a few things very clear:

* The argument over the feature did not establish an explicit measure of efficacy for the feature. The discussion struggled to even find relevant non-Toy code examples.

* The communication over the feature was almost entirely over email, even when it got extremely contentious. There was later some face-to-face talk at the summit.

* Guido stepped down.

Re:What's Coming in Python 3.8 by contravariant on Thursday July 18, 2019 @04:33AM (#20467909)
It may not have been a fair trade, but then it wasn't a trade in the first place. Those all seem to be problems with the process itself, meaning that it could have happened any time a contentious feature came up, this just happened to be the one to trigger the problem.
Re:What's Coming in Python 3.8 by choppaface on Thursday July 18, 2019 @07:46AM (#20468751)
I agree that a mess looked (sadly) inevitable based upon that post and some of the other surrounding context. E.g. Guido citing Yoda conditions, how dumb that game can be, and getting ignored.

But just because the feature shipped and design-by-committee is upon us doesn't mean we need to accept the outcome. Why couldn't there have been a more evolutionary path for this feature? For example, there is surely a way to write a prototype library to accomplish the same effect with slightly different syntax. (How about a single function `walrus(a, b)` that does what `:=` does?). Then let real user adoption drive the change. Maybe somebody will discover case statements from scala and want that instead.

I hope the committee models some amount of their work after WG21. C++ hasn't evolved so effectively because some people were magic visionaries. For the past decade, C++ has mostly ridden on the proven success of boost. And skipped a lot of the parts of boost that suck.

Re:What's Coming in Python 3.8 by heydenberk on Wednesday July 17, 2019 @06:46PM (#20464803)
I'd used f-string-like syntaxes in other languages before they came to Python. It was immediately obvious to me what the benefit would be.

I've used assignment expressions in other languages too! Python's version doesn't suffer from the JavaScript problem whereby equality and assignment are just a typo apart in, eg., the condition of your while loop. Nonetheless, I find that it ranges from marginally beneficial to marginally confusing in practice.

Re:What's Coming in Python 3.8 by fny on Wednesday July 17, 2019 @07:26PM (#20465073)
I love string interpolation! But this seems to take it to a bizarre level place just to save a few keystrokes. Seriously, how is f"{now=!s}" substantially better than f"now={str(now)}"?

Ergonomically, I see little benefit for the added complexity.

Re:What's Coming in Python 3.8 by sammorrowdrums on Thursday July 18, 2019 @02:03AM (#20467160)
It really feels like it's very explicit at this point that you want to cast whatever value interpolated into your format string to a string...

I don't want a type error for the most clear use case, in the same way I don't want one for print, because if I wanted a behaviour other than the basic string representation then I would still need to call something differently anyway.

Given that explicit control of the __str__ method is also baked into the language it's also very clear what to expect.

I like type errors when handling something like '1' + 1 because the JavaScript alternative is surprising and hides bugs. No surprises that a string formatter would coerce to string for me automatically (although I get that's maybe just personal feeling).

I love the f strings, they have made my codebase cleaner and clearer. Definitely a pythonic win.

Re:What's Coming in Python 3.8 by n3k5 on Thursday July 18, 2019 @06:32AM (#20468429)
In GP's example, the call to str() isn't there to make sure you get a string instead of causing a type error; it's there to substitute str() for repr() (which also returns a string). Considering that this feature is mainly meant to make 'print debugging' more convenient, it makes sense that repr() is the default choice.
Re:What's Coming in Python 3.8 by airstrike on Wednesday July 17, 2019 @07:28PM (#20465090)
Readability counts, though I agree Explicit is better than implicit.

Special cases aren't special enough to break the rules, Although practicality beats purity.

Re:What's Coming in Python 3.8 by hermitdev on Wednesday July 17, 2019 @09:34PM (#20465857)
I agree that the simple examples don't show much benefit, but imagine if you had a really complex expression. I can see the value there.
Re:What's Coming in Python 3.8 by agumonkey on Thursday July 18, 2019 @04:31AM (#20467897)
I'm trying to write an elisp macro because yes.. f-strings are too convenient
Re:What's Coming in Python 3.8 by Scarblac on Thursday July 18, 2019 @03:21AM (#20467536)
By itself I agree, every now and then you write a few lines that will be made a little shorter now that := exists.

But there's a long standing trend of adding more and more of these small features to what was quite a clean and small language. It's becoming more complicated, backwards compatibility suffers, the likelyhood your coworker uses some construct that you never use increases, there is more to know about Python.

Like f-strings, they are neat I guess. But we already had both % and .format(). Python is becoming messy.

I doubt this is worth that.

Re:What's Coming in Python 3.8 by vorticalbox on Thursday July 18, 2019 @03:25AM (#20467562)
they should pick f and put depreciation warnings on the other two, python is getting messy.
Re:What's Coming in Python 3.8 by uranusjr on Thursday July 18, 2019 @03:56AM (#20467727)
They can’t just deprecate the other two; f-string’s nature precludes it from being used in situations where formatting needs to occur lazily, e.g. i18n. This is the same reason why other languages with string interpolation also keep a format method around, e.g. Swift’s String(format:). I guess you could argue that they should at least deprecate %-formatting, and this has indeed been raised multiple times, even prior to f-string’s introduction, but the power of Backwards Compatibility Gods are still strong there, for better or for worse.
Re:What's Coming in Python 3.8 by heavenlyblue on Thursday July 18, 2019 @06:18AM (#20468371)
Nothing stops one from evaluating f-strings lazily. They could simply return a format string with parameters captured instead of interpolated string.
Re:What's Coming in Python 3.8 by hjk05 on Thursday July 18, 2019 @06:35AM (#20468441)
They could... but then they wouldn’t return the interpolated string, and they’d be just like using format, just saving the characters “.format” which ruins the exact thing people like about them.
Re:What's Coming in Python 3.8 by wodenokoto on Friday July 19, 2019 @01:35AM (#20476402)
Can you elaborate on the exact thing people like about f-strings, because I honestly thought it was saving having to write `format`
Re:What's Coming in Python 3.8 by heavenlyblue on Thursday July 18, 2019 @09:07AM (#20469329)
Nah - you're clearly not using Python enough - the issue with current format is that of the identity of the arguments passed to it.

I need to do 2 changes every time I change a format string - I need to remove the symbol representing it's placement and then I need to remove the argument passed to .format.

Also old formatting does not easily support arbitrary expressions in the placements, thus in order to get those you need to change the arguments passed to the .format.

f-strings get rid of those issues altogether. What you're showing is whatever you have in the brackets - 0 indirection and thus less margin for (unnecessary) errors.

Re:What's Coming in Python 3.8 by uranusjr on Thursday July 18, 2019 @12:40PM (#20471545)
I struggle to understand how what you talked about is relevant here. The problem parent talked about is how this laziness could be implemented without losing what makes f-string awesome right now. It’s not viable, which backs my reasoning why at least one alternative format method is required.

That said, I also struggle to understand how you’d claim parent clearly not using Python enough, when your description of str.format shows a lack of understanding to it yourself. One of the advantages of str.format over %-formatting is exactly that you do not need to modify the arguments passed to str.format when removing components from the source string:

    >>> '{0} {1} {2}'.format('a', 'b', 'c')
    'a b c'
    >>> '{0} {2}'.format('a', 'b', 'c')
    'a c'
Or preferably (using keyword arguments instead of positional):

    >>> '{x} {y} {z}'.format(x='a', y='b', z='c')
    'a b c'
    >>> '{x} {z}'.format(x='a', y='b', z='c')
    'a c'
But again, this doesn’t matter to parent’s argument. Nobody is arguing with you that f-string is better than alternatives for what it can do; we are trying to tell you that there are things it can’t do, and you did not get it.
Re:What's Coming in Python 3.8 by heavenlyblue on Sunday July 21, 2019 @06:59PM (#20494215)
>> One of the advantages of str.format over %-formatting is exactly that you do not need to modify the arguments passed to str.format when removing components from the source string:

It's not the string that most of the developers care about, it's the presence of the arguments to that string. The issue they are solving is "I would like to see A, B and C", rather than the issue of "I have provided A, B and C - would you please hide B from the view".

>> But again, this doesn’t matter to parent’s argument. Nobody is arguing with you that f-string is better than alternatives for what it can do; we are trying to tell you that there are things it can’t do, and you did not get it.

Please elaborate on what the f-string can't do? You have not provided the answer in your post. In my opinion, the only issue f-strings haven't solved is capturing the arguments in lambdas (before interplation) instead of their direct values. You, on the other hand - do not provide a clear explanation.

Re:What's Coming in Python 3.8 by ehsankia on Wednesday July 17, 2019 @06:59PM (#20464897)
Was the controversy really about the need for the feature? I thought most people agreed it was a great feature to have, and most of the arguments were about `:=` vs re-using `as` for the operator.
Re:What's Coming in Python 3.8 by pmart123 on Wednesday July 17, 2019 @07:54PM (#20465277)
I like "as" instead. I didn't realize that was on the table. To me, it seems more Pythonic given the typical English-like Python syntax of "with open(path) as file", "for element in items if element not in things", etc.
Re:What's Coming in Python 3.8 by mixmastamyk on Wednesday July 17, 2019 @08:10PM (#20465374)
It had some drawbacks, which a great majority of the time won't apply. I too would have preferred an "as" clause that limited to if-statements, maybe while, and comprehensions.
Re:What's Coming in Python 3.8 by Noumenon72 on Wednesday July 17, 2019 @11:04PM (#20466327)
"if m as re.match(p1, line)" is not very English-like.
Re:What's Coming in Python 3.8 by clinta on Wednesday July 17, 2019 @11:37PM (#20466474)
Because the variable goes after the as. if re.match(p1, line) as m:
Re:What's Coming in Python 3.8 by carver on Wednesday July 17, 2019 @11:23PM (#20466406)
`if re.match(p1, line) as m`, maybe?
Re:What's Coming in Python 3.8 by collinmanderson on Thursday July 18, 2019 @08:50AM (#20469199)
Yes, this is what I would expect. Much like a with statement.
Re:What's Coming in Python 3.8 by linsomniac on Wednesday July 17, 2019 @07:15PM (#20465000)
I don't know in this case, but I do know that the Python community tends to have strong opinions about things. The := resulted in Guido stepping down, which I think is a good indicator that there wasn't agreement that it was "a great feature to have" and just down to syntax... :-(
Re:What's Coming in Python 3.8 by dual_basis on Wednesday July 17, 2019 @07:51PM (#20465256)
To be fair, Guido stepped down because of the way the community reacted.

"The straw that broke the camel’s back was a very contentious Python enhancement proposal, where after I had accepted it, people went to social media like Twitter and said things that really hurt me personally. And some of the people who said hurtful things were actually core Python developers, so I felt that I didn’t quite have the trust of the Python core developer team anymore."

Source: https://www.infoworld.com/article/3292936/guido-van-rossum-r...

Note that Guido also was in support of the walrus operator, it's not like he stepped down because he disagreed with it.

Re:What's Coming in Python 3.8 by darkpuma on Wednesday July 17, 2019 @08:13PM (#20465391)
It's really disappointing that people could get so worked up, over what is essentially deciding which color to paint the shed, that they chase off the project founder. I wonder if there is any way for open source communities to effectively promote the "take a step back and remember what really matters in life" approach to conflict resolution.
Re:What's Coming in Python 3.8 by toyg on Wednesday July 17, 2019 @09:04PM (#20465671)
Guido wasn't chased off, he is still in the core group. He just isn't BDFL anymore -- which, from a certain point of view, might be the best of both worlds. As a long-time python user and small-time advocate, I felt the walrus was a really bad decision.
Re:What's Coming in Python 3.8 by Aozi on Thursday July 18, 2019 @08:02AM (#20468867)
>I wonder if there is any way for open source communities to effectively promote the "take a step back and remember what really matters in life" approach to conflict resolution.

We've spent decades debating on the merits of spaces vs tabs, and Vim vs Emacs, and a ton of other completely pointless stuff.

What you're hoping is just a pipe dream, people will get invested in the most petty and asinine stuff out there, and take it as a personal insult if you disagree.

Re:What's Coming in Python 3.8 by kbd on Wednesday July 17, 2019 @07:19PM (#20465030)
All discussion I've ever seen was about the need for the feature, not its spelling. I didn't even know "as" was proposed, but in fact it is an "alternate spelling" they considered[1] in the PEP.

[1] https://www.python.org/dev/peps/pep-0572/#alternative-spelli...

Re:What's Coming in Python 3.8 by mixmastamyk on Wednesday July 17, 2019 @08:12PM (#20465385)
The idea was accepted quickly, the rest of the debate was on its spelling and scope.
Re:What's Coming in Python 3.8 by linsomniac on Wednesday July 17, 2019 @07:11PM (#20464976)
I've literally been wanting something like the walrus operator since I first started using Python in '97. Mostly for the "m = re.match(x, y); if m: do_something()" sort of syntax.
Re:What's Coming in Python 3.8 by dual_basis on Thursday July 18, 2019 @12:47PM (#20471621)
I mean, that isolated example doesn't really demonstrate the benefit of a walrus operator does it? You could have just written "if re.match(x, y): do_something()". If you re-used the result of computation within the if statement, I feel that would be a better example, eg. "m = re.match(x, y); if m: do_something(m)".
Re:What's Coming in Python 3.8 by linsomniac on Friday July 19, 2019 @07:26AM (#20477686)
True enough, as you point out I would be expecting to do_something with m. :-)
Re:What's Coming in Python 3.8 by adito on Wednesday July 17, 2019 @07:43PM (#20465198)
I wonder if the controversial Go's error check function "try" proposal[0] will also be similar to this situation.

[0]: https://github.com/golang/go/issues/32437

Re:What's Coming in Python 3.8 by chasontherobot on Wednesday July 17, 2019 @07:47PM (#20465233)
That was already cancelled.
Re:What's Coming in Python 3.8 by brummm on Thursday July 18, 2019 @08:56AM (#20469230)
I think in certain situations the walrus operator will probably be useful. But it definitely makes code less legible, which makes me cautious. The only useful use case I have found so far is list comprehensions where some function evaluation could be reduced to only one execution with the walrus operator.
Re:What's Coming in Python 3.8 by kbd on Friday July 19, 2019 @02:55PM (#20481471)
> But it definitely makes code less legible, which makes me cautious.

Disagree. In cases where it's useful it can make the code much clearer. Just yesterday I wrote code of the form:

    foos = []
    foo = func(a,b,c,d)
    while foo:
       foos.append(foo)
       foo = func(a,b,c,d)
With the walrus operator, that would just be:

    foos = []
    while foo := func(a,b,c,d):
        foos.append(foo)
Further, I had to pull out 'func' into a function in the first place so I wouldn't have something complicated repeated twice, so it would remove the need for that function as well.
Re:What's Coming in Python 3.8 by craigds on Thursday July 18, 2019 @07:27PM (#20474980)
Yep, I can't wait to use the walrus operator. I just tried it out (`docker run -it python:3.8.0b2-slim`) and I'm hooked already.

Also, it's times like these I'm really glad docker exists. Trying that out before docker would have been a way bigger drama