News for hackers. Stuff that matters.
Re:Be Careful with Python's New-Style String Format by bubblesocks on Saturday December 31, 2016 @04:50PM (#13293369)
The nice thing about the new format is that you can reverse the order of the variables inside the string, which is nice when you're localizing strings into languages with a reversed sentence structures, like Japanese. I haven't done a ton of Python, but the old style won't let you do that, will it?
Re:Be Careful with Python's New-Style String Format by Franciscouzo on Saturday December 31, 2016 @05:08PM (#13293454)
Reversing the order is not possible, but you can use a dict instead:

    >>> "%(foo)s" % {"foo": "bar"}
    'bar'
Re:Be Careful with Python's New-Style String Format by dozzie on Sunday January 01, 2017 @01:54AM (#13294951)
Funny. I thought Python implemented all the quirks of the original printf() formatting, including using arguments at specific positions. Apparently it didn't (and arguably it's a good thing).

  $ printf '%2$s %2$s %1$s\n' aaa bbb
  bbb bbb aaa
Re:Be Careful with Python's New-Style String Format by libeclipse on Saturday December 31, 2016 @06:50PM (#13293843)
Heyyyyyy I did not know that. TIL, thank you.
Re:Be Careful with Python's New-Style String Format by nitely on Saturday December 31, 2016 @05:16PM (#13293499)
Yes, it will, just use named placeholders: "%(some)s" % {'some': 'helllo'}
Re:Be Careful with Python's New-Style String Format by tedmiston on Saturday December 31, 2016 @07:07PM (#13293925)
I'm not sure I understand the use case for reversing the formatting variables, but you could do this to make old-style formatting work that way:

    >>> 'hello, %s %s' % tuple(reversed(['smith', 'bob']))
    'hello, bob smith'
I think it's more readable if you abstract that bit into a lambda function:

    >>> rev_args = lambda *args: tuple(reversed(args))
    >>> 'hello, %s %s' % rev_args('smith', 'bob')
    'hello, bob smith'