News for hackers. Stuff that matters.
Re:What’s New in Python 3.8 by airstrike on Monday October 14, 2019 @05:57PM (#21253327)
The expansion of f-strings is a welcome addition. The more I use them, the happier I am that they exist

https://docs.python.org/3/whatsnew/3.8.html#f-strings-suppor...

Re:What’s New in Python 3.8 by nostrademons on Monday October 14, 2019 @06:24PM (#21253525)
I saw that and was like "Oh, that'd be a handy feature for a lot of other programming languages too." But the more I think about it, the more I'd rather have a feature that takes a list of expressions and converts it into a dict where the keys are the expression text and the values are the values of those expressions. Basically, syntactic sugar for this:

  { k: eval(k) for k in ('theta', 'delta.days', 'cos(radians(theta))') }
This would trivially subsume the f'{user=}' syntax for the example given: just print out the dictionary. But it'd also be useful for: filling template dictionaries; printing out status pages for HTTP webservers; returning multiple variables from a function; flattening out complex data structures; creating dispatch tables out of local functions.

You could even have a syntax like locals('theta', 'delta.days') and keep it familiar.

Re:What’s New in Python 3.8 by elcritch on Tuesday October 15, 2019 @03:34AM (#21256319)
Pretty trivial to do with proper macros in a language. Julia has macros for printing variables and values, like `@debug(var)`.

There's an Elixir library with macros to make a map (dictionary) using the variable names passed in [1]:

    iex> import ShorterMaps
    ...> name = "Chris"
    ...> id = 6
    ...> ~M{name, id}
       %{name: "Chris", id: 6}
Though wether its a good idea or not is another question. ;) If you want to do the type of programming you're talking about you should try Elixir/Julia/Clojure(/Rust?)... or any number of other languages with macros.

1: https://github.com/meyercm/shorter_maps

Re:What’s New in Python 3.8 by airstrike on Tuesday October 15, 2019 @12:16PM (#21260146)
Can't you build your own function using format()? f-strings are just syntactic sugar for format() so presumably you can
Re:What’s New in Python 3.8 by skunkworker on Monday October 14, 2019 @06:12PM (#21253433)
This makes clean string interpolation so much easier to do, especially for print statements. It's almost hard to use python < 3.6 now because of them.
Re:What’s New in Python 3.8 by heavenlyblue on Monday October 14, 2019 @06:10PM (#21253414)
One thing I vastly dislike with them is that they are still expanded in logging, while they are not expanded when logging is using "the old way".
Re:What’s New in Python 3.8 by Will_Do on Monday October 14, 2019 @06:14PM (#21253445)
What do you mean the old way?

Will it print what foo is if I type logger.info("{foo=}")?

Re:What’s New in Python 3.8 by zaphirplane on Monday October 14, 2019 @06:34PM (#21253586)
Logging.info(“%s %s”, name, country”) the string isn’t expanded if the logging is set to warn. While with your example the string is expanded then discarded
Re:What’s New in Python 3.8 by falcor84 on Monday October 14, 2019 @06:16PM (#21253461)
How would you fix the issue without breaking existing semantics?

The way I see it, it's a necessary price to pay, and I just continue to use c-style forming strings in logging.

Re:What’s New in Python 3.8 by eru on Tuesday October 15, 2019 @02:27AM (#21256015)
The clean way would be to introduce laziness. But yeah, can't do that in Python.
Re:What’s New in Python 3.8 by bhl on Monday October 14, 2019 @11:40PM (#21255358)
Why didn’t Python ship with the opposite functionality as well? Parsing instead of formatting. Given a string and a format string, return a list of variables (or a dictionary).
Re:What’s New in Python 3.8 by rcfox on Tuesday October 15, 2019 @12:15AM (#21255511)
Re:What’s New in Python 3.8 by ash on Tuesday October 15, 2019 @09:27AM (#21258101)
Are you talking about parsing f-strings? How would that look like?

As far I understand the following:

    print(f"your name is {name}")
is roughly equivalent to:

    print(f"your name is " + format(name))
What is there to parse?
Re:What’s New in Python 3.8 by bhl on Tuesday October 15, 2019 @11:35AM (#21259560)
For example, to checkpoint a model, I would save it as “ckpt-{epoch_number}-{val_loss}”. Given this file name and the original format string, I would like to recover the epoch number and validation loss variables back.

From: ckpt-8-0.300 To: epoch=8, val_loss=0.300

Re:What’s New in Python 3.8 by bhy on Tuesday October 15, 2019 @04:48AM (#21256662)
I think this is what you want:

https://github.com/r1chardj0n3s/parse