News for hackers. Stuff that matters.
Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @08:03PM (#10833454)
From another post:

    "{a} {b} {a}".format(a=a, b=b)
    "{a} {b} {a}".format(**locals())
Compared to this:

    f"{a} {b} {a}"
Sorry that's about 1000% better. This should have been the one way to do it, originally. It isn't magic either, rather a simple compile-time transformation to existing format syntax. There's nothing new to remember besides a large reduction in noise.
Re:New string formatting in Python by schmichael on Sunday January 03, 2016 @08:51PM (#10833597)
I'm in the "wish it was more explicit" camp. Would a fmt function be that terrible?

    fmt("{a} {b} {c}", a=a, b=b, c=c)
If you want to save typing, maybe use :a instead of {a}. Or ?a would have made plain old ? a nice positional variant:

    fmt("?a ? ?", a=a, b, c)
The main benefit of the fmt function is that it requires no syntax changes to the language and is trivially provided by a third party library for all past versions of Python.

That being said this ship has sailed. I guess I just take a more conservative approach to syntax changes than most.

Update: a bit sad to see my votes fluctuating wildly on this post. Please don't use votes to support or disagree with me: that's not what they're for. Please vote only based on whether you find this relevant.

Re:New string formatting in Python by scrollaway on Sunday January 03, 2016 @09:49PM (#10833764)
There's a fantastic "more explicit" version of it: str.format. This really is just an implicit version of str.format(). I just don't understand why such a significant syntax change would make it into python when it deviates so much from their usual mantra.
Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @11:08PM (#10833941)
Guido is not a purity robot, and has been looking for a way to have this feature since perl and python were duking it out in the early nineties. Practicality beats purity.

It isn't a large syntax change, string prefixes have existed since the beginning.

Re:New string formatting in Python by sametmax on Monday January 04, 2016 @02:41AM (#10834437)
Yes. "Practicality beats purity". This part of the zen of Python, too many people forget about it.

People forget that Python is also about readability, and ease of code exploration is the shell. Fstring is a feature to make that even better.

Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @08:58PM (#10833617)
It is explicit, in that one must use the f'' prefix. There are no syntax changes as u'', b'', and r'' already exist. Positionals make bugs more likely.
Re:New string formatting in Python by schmichael on Sunday January 03, 2016 @09:01PM (#10833628)
It is implicit in that the interpolated values are specified anywhere in scope, not inline as with every other formatting option.

I consider specifying the values or variables alongside the formatting string a requirement to be considered explicit, but I can see how it's a matter of opinion.

Re:New string formatting in Python by sametmax on Monday January 04, 2016 @02:45AM (#10834448)

    @decorator
    def decorated
Is doing:

    decorated = decorator(decorated)
The switch is implicit too. It's syntaxic sugar to gain a pratical and elegant syntax for a common use case.

It's not going to introduce vulnerability. It's going to make your code easier to write and read. It's going to make bug easier to spot in formating. It's going to make shell sessions easier.

Re:New string formatting in Python by schmichael on Monday January 04, 2016 @02:43PM (#10837965)
The manipulation (@dec) is still right next to the thing being manipulated (def dec).

I'm not trying to be pedantic. It really does affect readability when syntactic sugar's affect spans an entire scope. Whether or not that effect on readability is greater or less than the gain by the syntactic sugar is always a matter of opinion. Obviously my opinion is out of line with Python's core devs.

Re:New string formatting in Python by schmichael on Sunday January 03, 2016 @09:02PM (#10833634)
It is a syntax change as before f"" was a syntax error:

    >>> f"foo"
      File "<stdin>", line 1
        f"foo"
             ^
    SyntaxError: invalid syntax
Re:New string formatting in Python by schmichael on Sunday January 03, 2016 @09:23PM (#10833693)
> ... though similar to adding a function that didn't exist before.

This is not true and exactly the distinction I'm trying to make:

Adding new packages, functions, objects, etc. can all be backported to older versions and alternative implementations. They also require no updates to ASTs, linters, syntax highlighters, static analysis tools etc.

Adding new syntax is backward incompatible (unless it's added as a from __future__ import to new old releases) and requires changes to all tools that parse Python syntax (the interpreter, ASTs, linters, transpilers, etc).

Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @10:52PM (#10833902)
Ok, true, though I would argue that it isn't entirely new syntax but rather a variation of an existing one.

It is a shame that linters will have to add a letter to their grammar also, but I argue that the everyday usability and readability for millions will outweigh this drawback.

Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @09:14PM (#10833666)
I suppose technically true, though similar to adding a function that didn't exist before. The syntax extension part of the feature (adding a letter to the grammar) is minuscule.
Re:New string formatting in Python by pvg on Monday January 04, 2016 @01:18AM (#10834284)
Re:New string formatting in Python by schmichael on Monday January 04, 2016 @01:24AM (#10834300)
Ha, well, I was I wrong. Thanks for the correction.
Re:New string formatting in Python by meowface on Sunday January 03, 2016 @08:35PM (#10833555)
Some people deride string interpolation as not explicit enough, but I think it's extremely readable and adds clarity. Plus, with the `f` prefix, it's plenty explicit IMO.
Re:New string formatting in Python by MereInterest on Sunday January 03, 2016 @08:44PM (#10833580)
String interpolation is only being added for string literals, not for generic strings. This means that you still wouldn't be able to read a template from a file, then format text in.

    with open('template.txt') as f:
        template = f.read()
    formatted = template.format(**values)
Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @08:47PM (#10833585)
That's a good thing due to security reasons as arbitrary expressions are allowed. There are plenty of templating solutions available.
Re:New string formatting in Python by Animats on Sunday January 03, 2016 @10:31PM (#10833861)
Arbitrary string interpolation was proposed, in a way sure to result in security problems:

"PEP 498 proposes new syntactic support for string interpolation that is transparent to the compiler, allow name references from the interpolation operation full access to containing namespaces (as with any other expression), rather than being limited to explicit name references. These are referred to in the PEP as "f-strings" (a mnemonic for "formatted strings")."

"Full access to containing namespaces?" From strings? Bad, bad idea. This is currently marked as "deferred", but should be marked "rejected with extreme prejudice".

Re:New string formatting in Python by mixmastamyk on Sunday January 03, 2016 @11:16PM (#10833962)
FUD: applies to literals only. There is nothing that could happen in the string that couldn't happen in your code already. No new security concerns.
Re:New string formatting in Python by Animats on Sunday January 03, 2016 @11:23PM (#10833974)
Example given:

    myquery = sql(i"SELECT {column} FROM {table};")
What could possibly go wrong?
Re:New string formatting in Python by icebraining on Monday January 04, 2016 @06:15AM (#10834910)
How is that any different from any other string formatting mechanism?

  myquery = sql("SELECT %s FROM %s" % (column, table))
Re:New string formatting in Python by berdario on Monday January 04, 2016 @06:43AM (#10834998)
plenty of things can go wrong, since you have control of how many and which variables to interpolate, e.g:

    i"SELECT {settings.SECRET_KEY};"
PS: (I don't know why but HN is not updating the page with the reply links needed, so I'll just edit this)

Yes, I agree. I think that the misunderstanding happened when mixmastamyk wrote

> That's a good thing due to security reasons as arbitrary expressions are allowed. There are plenty of templating solutions available

The point is that even if you don't allow arbitrary expressions (which imho are a mistake, and of which I haven't seen a single use case yet), having this kind of interpolation from strings that are not literals (i.e. are not in the trusted source code) would still be a security issue

Since the PEPs apparently don't propose to extend this to non-literals, we're safe. But it's better to be wary and attentively review such proposals...

In fact, I just realized right now that Animats might have misunderstood PEP 501, since

    sql(i"SELECT {column} FROM {table};")
should be perfectly safe from sqli vulns

PPS: Unless Animats is pointing out how switching i'' for f'' is a terribly simple mistake to do and hard to spot during a code review... I agree with that

Re:New string formatting in Python by icebraining on Monday January 04, 2016 @06:49AM (#10835024)
But again, if you can inject that into the source code, why can't you just do

  "SELECT {};".format(settings.SECRET_KEY)
Remember that the interpolation only works for string literals, you can't inject that from external input.
Re:New string formatting in Python by MereInterest on Sunday January 03, 2016 @08:49PM (#10833591)
I agree. I was pointing out that the f'{a} {b}' syntax can't accomplish everything. Since it isn't intended as a complete replacement, the way `.format` is intended as a complete replacement for `%`, it will just add another string formatting technique without ever deprecating the old way.
Re:New string formatting in Python by Walkman on Sunday January 03, 2016 @08:49PM (#10833592)
Nothing wrong with this, because it's explicit. You could't be mo terse in this case, could you?
Re:New string formatting in Python by kevin_thibedeau on Monday January 04, 2016 @12:46AM (#10834213)
This breaks gettext translations:

  _(f"English {a} words {b} here {a}")
The interpolation is done before the string is passed to gettext which can't retrieve the translated string any more.
Re:New string formatting in Python by mixmastamyk on Monday January 04, 2016 @12:25PM (#10836892)
Correct, don't use it with i18n, the same as with any other string formatting.