News for hackers. Stuff that matters.
Re:Asterisks in Python by avbor on Thursday October 11, 2018 @05:13PM (#18197082)
In python 3.6 and up, that becomes

    def example_cheater(color, flavor, age):
      return f'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream'
Even nicer!
Re:Asterisks in Python by dec0dedab0de on Thursday October 11, 2018 @05:18PM (#18197123)
OMG! that is so exciting, Thank you for telling me! I have only been using 3.x as my primary language for a few months now, I can't wait to try this out. I feel like a huge dork right now, because that made my day.
Re:Asterisks in Python by luhn on Thursday October 11, 2018 @05:16PM (#18197111)
For reference, this feature is Formatted String Literals ("f-strings"). https://docs.python.org/3/reference/lexical_analysis.html#fo...
Re:Asterisks in Python by shaklee3 on Thursday October 11, 2018 @11:43PM (#18199029)
Of course I didn't read your comment until several Google searches later.
Re:Asterisks in Python by chestervonwinch on Thursday October 11, 2018 @09:00PM (#18198312)
Neat:

    >>> pi = 3.14159; two = 2
    >>> f"{two:02d} pi is {2*pi:.2f}"
    '02 pi is 6.28'
Re:Asterisks in Python by nazgul17 on Thursday October 11, 2018 @05:17PM (#18197118)
Didn't know this. Thanks for sharing!
Re:Asterisks in Python by sys_64738 on Thursday October 11, 2018 @07:48PM (#18197967)
Does this trip up pylint?
Re:Asterisks in Python by aportnoy on Thursday October 11, 2018 @11:04PM (#18198845)
Trips up Vim's syntax highlighter.
Re:Asterisks in Python by JoBrad on Thursday October 11, 2018 @10:23PM (#18198652)
Hasn’t for me.
Re:Asterisks in Python by nercht12 on Thursday October 11, 2018 @08:34PM (#18198201)
Taken straight out of Ruby, I see. Very convenient feature (and one of my favorites about Ruby), though it does contradict the do-it-one-way mentality.
Re:Asterisks in Python by bhrgunatha on Thursday October 11, 2018 @09:55PM (#18198528)
According to Rosetta Code Algol 68 had string interpolation. [1]

It'd be interesting to see if there are any older languages that supported it too.

https://rosettacode.org/wiki/String_interpolation_(included)...

Re:Asterisks in Python by setr on Friday October 12, 2018 @02:11AM (#18199614)
>though it does contradict the do-it-one-way mentality.

not really: use f-strings when you're passing in variables as-is (or nearly no work), and format() when you need to do work on them before stringifying them; f-strings are naturally (and obviously) more difficult to read when the variables are big, as it obscures the actual text they're being fit into, and where.

The only natural area for preference to apply is whether to do the work before the format() call, name the variables, and change it to an f-string.. or stick with a multi-line format()

I'm not sure theres any real situation where the choice isn't obvious. Maybe if you're doing something like f"list1: {sorted(a)}\nlist2{sorted(b)}", where the work is rather small, but even then f"list1:{}\nlist2:{}".format(sorted(a), sorted(b)) is just as nice, or rather unsatisfying, as the f-string.

Re:Asterisks in Python by nercht12 on Friday October 12, 2018 @12:55PM (#18202781)
> not really:

Yes it does. You could construct the string using a for-loop. Thank you for proving my point and then down-voting me.

Re:Asterisks in Python by setr on Friday October 12, 2018 @02:03PM (#18203341)
...if you’re going down that route, you could claim that any feature not necessary for turing completeness conflicts with the one way to do it strategy, since you could alternatively build up whatever feature from scratch, thus offering additional solutions

But practically, there remains (close to) one obvious way to do things, with the choice boiling down to whether or not to name your outputs before string construction (and the choice of f-string vs format naturally falling out). But thats always been a choice.

On a side note: I don’t use comment voting systems, for up or down votes, on pretty much any site including HN. Even if I did, I don’t see why you’d care

Re:Asterisks in Python by nercht12 on Friday October 12, 2018 @09:15PM (#18205825)
> if you’re going down that route, you could claim that any feature not necessary for turing completeness conflicts with the one way to do it strategy

I suppose one could do that, philosophically if not practically. What I had in mind was the various syntax sugars used for the same thing, particularly in Ruby. While some ways of doing things in Ruby were convenient, there was a host of other "shortcuts" that added to the confusion, and it was evident that it was the design of the language itself - not an accident - that allowed these shortcuts, at least imo.

> On a side note: I don’t use comment voting systems

I had a feeling you might say that, but I'd already clicked the "reply" button. Sorry for being presumptuous.

Not that I care about the voting system either, but people tend to use it as a cowardly way of showing disapproval.

Re:Asterisks in Python by masklinn on Friday October 12, 2018 @06:19AM (#18200415)
f-strings are also unusable for i18n.