News for hackers. Stuff that matters.
Re:Python’s tug of war between beginner-friendly and advance... by arthurcolle on Thursday February 04, 2021 @08:45PM (#26032111)
f-strings are amazing

Can we make that the standard and get rid of the ugly str.format(UGLINESS) syntax.

Yes please, and thank you

Re:Python’s tug of war between beginner-friendly and advance... by pansa2 on Thursday February 04, 2021 @08:51PM (#26032148)
`str.format` can’t be removed completely - it’s necessary if you want to build format strings at runtime.

But otherwise, yes - make f-strings the “one obvious way to do it” and use `format` only when necessary. And get rid of the old %-formatting.

Also consider making f-strings the default: https://pyfound.blogspot.com/2020/04/all-strings-become-f-st...

Re:Python’s tug of war between beginner-friendly and advance... by woodruffw on Thursday February 04, 2021 @08:58PM (#26032181)
> Can we make that the standard and get rid of the ugly str.format(UGLINESS) syntax.

`format` is still useful, particularly when working with pre-templatized strings. It's also a (relatively) new feature; Python 2 didn't have it at all.

OTOH, I wouldn't cry if Python 3.X finally removed %-formatting. `format` does everything that `%` does, with a much nicer API (especially in 3.7+).

Re:Python’s tug of war between beginner-friendly and advance... by roelschroeven on Friday February 05, 2021 @05:28AM (#26034839)
I'm not a big fan of f-strings. I like to have a nice separation between the format and the values.

f-strings work well in simple cases. But once the things you want to output are somewhat larger expressions, format() works much better.

Example: instead of something like

    print(f'{id:<5}  |  {", ".join(members}:<50} |  {", ".join(str(score) for score in scores):<30}')
This is much clearer IMO:

    print('{:<5}  |  {:<50} |  {:<30}'.format(
        id,
        ", ".join(members),
        ", ".join(str(score) for score in scores),
        ))
Actually, what do you mean by "ugly" and "UGLINESS" in "the ugly str.format(UGLINESS) syntax"? The UGLINESS are simply the things you want to format. Ugly is in the of the beholder of course; personally I don't really see anything ugly about format. The f-string equivalent here is much uglier and much less clear/readable to me, because the formatting and the expressions are mixed and parsing the whole thing to see what is what is more effort than it should be.
Re:Python’s tug of war between beginner-friendly and advance... by shaded-enmity on Friday February 05, 2021 @08:11AM (#26035899)
Both examples look unreadable to me. Just because you can do a lot on a single line doesn't mean you should.

    all_members = ", ".join(members)
    all_scores = ", ".join(map(str, scores))
    print(f'{id:<5} {all_members:<50} {all_scores:<30}')
Re:Python’s tug of war between beginner-friendly and advance... by dragonwriter on Friday February 05, 2021 @06:40AM (#26035264)
> Can we make that the standard and get rid of the ugly str.format(UGLINESS) syntax.

No, please.

F-strings are useful for some things, but there's a good reason. to define templates other than deep in code for ease of editing them distinct from functionality (often, completely externally to code) and using str.format supports that.

If anything, I'd prefer r-strinfs as the default with both escape handling and formatting non-default options, but the status quo is acceptable and better than r-strings as the base.

Re:Python’s tug of war between beginner-friendly and advance... by kortex on Thursday February 04, 2021 @11:40PM (#26033200)
Jokes on you, my coworker still obsessively uses % args syntax.
Re:Python’s tug of war between beginner-friendly and advance... by roelschroeven on Friday February 05, 2021 @05:31AM (#26034861)
I can understand that, to a point. Not the so much the obsessive part, but I sometimes fall back to % formatting too since that uses the formatting syntax I'm familiar with from C and C++. The syntax used in f-strings and .format is probably better, but requires re-learning.
Re:Python’s tug of war between beginner-friendly and advance... by CodeGlitch on Saturday February 06, 2021 @11:34AM (#26047629)
Please tell me you correct them in the code reviews?

Everytime someone uses %args inappropriately a baby seal dies.

Re:Python’s tug of war between beginner-friendly and advance... by enriquto on Friday February 05, 2021 @02:56AM (#26034093)
f-strings are standard, what do you mean? That the "f" be ommitted? I'm all for it, yet it would need a bit of adaptation for those old programs that put {} inside non-f strings.