• on str.format and f-strings

    From Meredith Montgomery@21:1/5 to All on Mon Sep 5 16:07:57 2022
    It seems to me that str.format is not completely made obsolete by the
    f-strings that appeared in Python 3.6. But I'm not thinking that this
    was the objective of the introduction of f-strings: the PEP at

    https://peps.python.org/pep-0498/#id11

    says so explicitly. My question is whether f-strings can do the
    following nice thing with dictionaries that str.format can do:

    --8<---------------cut here---------------start------------->8---
    def f():
    d = { "name": "Meredith", "email": "mmontgomery@levado.to" }
    return "The name is {name} and the email is {email}".format(**d) --8<---------------cut here---------------end--------------->8---

    Is there a way to do this with f-strings?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Meredith Montgomery on Mon Sep 5 19:33:31 2022
    Meredith Montgomery <mmontgomery@levado.to> writes:
    ...
    d = { "name": "Meredith", "email": "mmontgomery@levado.to" }
    return "The name is {name} and the email is {email}".format(**d)
    --8<---------------cut here---------------end--------------->8---
    Is there a way to do this with f-strings?

    I cannot think of anything shorter now than:

    eval( 'f"The name is {name} and the email is {email}"', d )

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Meredith Montgomery@21:1/5 to Stefan Ram on Mon Sep 5 17:18:31 2022
    ram@zedat.fu-berlin.de (Stefan Ram) writes:

    Meredith Montgomery <mmontgomery@levado.to> writes:
    ...
    d = { "name": "Meredith", "email": "mmontgomery@levado.to" }
    return "The name is {name} and the email is {email}".format(**d) >>--8<---------------cut here---------------end--------------->8---
    Is there a way to do this with f-strings?

    I cannot think of anything shorter now than:

    eval( 'f"The name is {name} and the email is {email}"', d )

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    .

    Lol. That's brilliant! Thanks very much!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Meredith Montgomery@21:1/5 to Julio Di Egidio on Mon Sep 5 20:02:42 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote:
    r...@zedat.fu-berlin.de (Stefan Ram) writes:

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    Lol. That's brilliant! Thanks very much!

    Calling eval for that is like shooting a fly with a cannon.

    Indeed! But we're not looking for production-quality code. Just an
    extreme way to satisfy a silly requirement.

    Besides, this one is even shorter:

    f"The name is {d['name']} and the email is {d['email']}"

    This doesn't quite satisfy the requeriments. We're trying to specify
    only the keys, not the dictionary. (But maybe the requirements did not
    say that explicitly. I'd have to look it up again --- it's been
    snipped. It's not important. Thanks much for your thoughts!)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Meredith Montgomery on Mon Sep 5 15:51:43 2022
    On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote:
    r...@zedat.fu-berlin.de (Stefan Ram) writes:

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    Lol. That's brilliant! Thanks very much!

    Calling eval for that is like shooting a fly with a cannon.

    Besides, this one is even shorter:

    f"The name is {d['name']} and the email is {d['email']}"

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Meredith Montgomery on Mon Sep 5 16:28:33 2022
    On Tuesday, 6 September 2022 at 01:03:02 UTC+2, Meredith Montgomery wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote:
    r...@zedat.fu-berlin.de (Stefan Ram) writes:

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    Lol. That's brilliant! Thanks very much!

    Calling eval for that is like shooting a fly with a cannon.

    Indeed! But we're not looking for production-quality code. Just an
    extreme way to satisfy a silly requirement.

    Indeed, as far as programming goes, even the premise is
    totally nonsensical. Maybe you too better go to the pub?

    Besides, this one is even shorter:

    f"The name is {d['name']} and the email is {d['email']}"

    This doesn't quite satisfy the requeriments. We're trying to specify
    only the keys, not the dictionary. (But maybe the requirements did not
    say that explicitly. I'd have to look it up again --- it's been
    snipped. It's not important. Thanks much for your thoughts!)

    So, here is what you said:

    <quote>
    It seems to me that str.format is not completely made obsolete by the
    f-strings that appeared in Python 3.6.
    </quote>

    No, of course they can do the same things.

    <quote>
    My question is whether f-strings can do the
    following nice thing with dictionaries that str.format can do:

    def f():
    d = { "name": "Meredith", "email": "mmont...@levado.to" }
    return "The name is {name} and the email is {email}".format(**d)

    Is there a way to do this with f-strings?
    </quote>

    To which I have answered above: and you even save few
    characters...

    That said, two notes, which is really why I felt compelled
    to chime in:

    1) saving few characters is always completely and utterly
    counter-productive, what you want is readability, if anything;

    2) using eval and co. not to type a few characters is plain
    abominable.

    Never mind, just should anybody be actually interested in
    programming.

    Have fun,

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Meredith Montgomery@21:1/5 to Julio Di Egidio on Mon Sep 5 22:07:25 2022
    Julio Di Egidio <julio@diegidio.name> writes:

    On Tuesday, 6 September 2022 at 01:03:02 UTC+2, Meredith Montgomery wrote:
    Julio Di Egidio <ju...@diegidio.name> writes:
    On Monday, 5 September 2022 at 22:18:58 UTC+2, Meredith Montgomery wrote: >> >> r...@zedat.fu-berlin.de (Stefan Ram) writes:

    , but with the spaces removed, it's even one character
    shorter than the format expression:

    eval('f"The name is {name} and the email is {email}"',d)
    "The name is {name} and the email is {email}".format(**d)

    Lol. That's brilliant! Thanks very much!

    Calling eval for that is like shooting a fly with a cannon.

    Indeed! But we're not looking for production-quality code. Just an
    extreme way to satisfy a silly requirement.

    Indeed, as far as programming goes, even the premise is
    totally nonsensical. Maybe you too better go to the pub?

    It surely isn't precise, but Stefan Ram caught my meaning. It's hard to
    be precise. I wanted to avoid having to write things like d['key'].
    Stefam Ram provided a solution. I did not care whether it was something sensible to do in Python from a serious-programming perspective. Thank
    you for thoughts anyhow. I appreciate it.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Meredith Montgomery on Wed Sep 7 03:57:03 2022
    On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmontgomery@levado.to> wrote:

    It seems to me that str.format is not completely made obsolete by the f-strings that appeared in Python 3.6. But I'm not thinking that this
    was the objective of the introduction of f-strings: the PEP at

    https://peps.python.org/pep-0498/#id11

    says so explicitly.

    Precisely. It was never meant to obsolete str.format, and it does not.

    My question is whether f-strings can do the
    following nice thing with dictionaries that str.format can do:

    --8<---------------cut here---------------start------------->8---
    def f():
    d = { "name": "Meredith", "email": "mmontgomery@levado.to" }
    return "The name is {name} and the email is {email}".format(**d) --8<---------------cut here---------------end--------------->8---

    Is there a way to do this with f-strings?

    No. That's not their job. That's str.format's job.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Meredith Montgomery@21:1/5 to Chris Angelico on Tue Sep 6 16:07:08 2022
    Chris Angelico <rosuav@gmail.com> writes:

    On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmontgomery@levado.to> wrote:

    It seems to me that str.format is not completely made obsolete by the
    f-strings that appeared in Python 3.6. But I'm not thinking that this
    was the objective of the introduction of f-strings: the PEP at

    https://peps.python.org/pep-0498/#id11

    says so explicitly.

    Precisely. It was never meant to obsolete str.format, and it does not.

    My question is whether f-strings can do the
    following nice thing with dictionaries that str.format can do:

    --8<---------------cut here---------------start------------->8---
    def f():
    d = { "name": "Meredith", "email": "mmontgomery@levado.to" }
    return "The name is {name} and the email is {email}".format(**d)
    --8<---------------cut here---------------end--------------->8---

    Is there a way to do this with f-strings?

    No. That's not their job. That's str.format's job.

    Chris! So good to see you around here again. Thank you so much for
    your input on this.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Julio Di Egidio@21:1/5 to Chris Angelico on Tue Sep 6 23:49:29 2022
    On Tuesday, 6 September 2022 at 19:57:36 UTC+2, Chris Angelico wrote:
    On Wed, 7 Sept 2022 at 03:52, Meredith Montgomery <mmont...@levado.to> wrote:

    It seems to me that str.format is not completely made obsolete by the f-strings that appeared in Python 3.6. But I'm not thinking that this
    was the objective of the introduction of f-strings: the PEP at

    https://peps.python.org/pep-0498/#id11

    says so explicitly.
    Precisely. It was never meant to obsolete str.format, and it does not.
    My question is whether f-strings can do the
    following nice thing with dictionaries that str.format can do:

    --8<---------------cut here---------------start------------->8---
    def f():
    d = { "name": "Meredith", "email": "mmont...@levado.to" }
    return "The name is {name} and the email is {email}".format(**d) --8<---------------cut here---------------end--------------->8---

    Is there a way to do this with f-strings?

    No. That's not their job. That's str.format's job.

    What is not their job, you other resident cretin !?

    You fraudulent polluting morons, get extinguished already...

    *Plonk*

    Julio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)