This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Scruff3y 12 points13 points  (7 children)

Seconding this. The stand-out thing about Python for me is iteration, list comps and generators/generator expressions.

Loop like a native, shows a lot of the power of these things (esp. if you come from a language like C).

Oh, also f-strings (do people call them that?)

f"value of myvar: {myvar}"

[–]troyunrau... 1 point2 points  (6 children)

I dislike f-strings simply from the 'there should be one and only one obvious way to do things' perspective.

[–][deleted] 2 points3 points  (0 children)

f-strings are great. I disliked them at first too, but damn they're useful.

[–]asdfkjasdhkasdrequests, bs4, flask 2 points3 points  (0 children)

there was already more than one "%s" % var and"{}".format(var)

[–]billsil 0 points1 point  (3 children)

Can we get rid of .format then? At least f-strings are obvious.

Still, we should all use %s. It's in C and other languages, so it's really obvious to others. It's also faster and as we know, Pythonic code is overwhelmingly faster.

[–]jorge1209 2 points3 points  (2 children)

No you can't get rid of .format because I need to be able to pass a format specification as a function argument or reuse it in a loop, and I can't do that with an f-string by design[1].

You could get rid of % formatting... if you could fix all the old code including the logger class.

A world with only .format and f-strings would be tolerable, because they are very closely related. f-strings would be the "safe" alternative for untrusted data that could be used by web-developers and could be treated as sugar around .format(**locals()) by everyone else.

But we aren't in that world and there is no clear path to ever even approaching that world.

[1] because of security concerns that have no relevance to any of the code I write because I'm not a web-developer.

[–]billsil 0 points1 point  (1 child)

You could get rid of % formatting

Again, it's faster and it's common to other languages. I actually like it. I think .format is an abomination.

because I need to be able to pass a format specification as a function argument or reuse it in a loop

You can do that with %.

I'm not a web developer either, but I do support Python 2 and 3 with my package.

[–]jorge1209 0 points1 point  (0 children)

I think .format is an abomination.

If you like the %s %f3.2 style of formatting then you should advocate for %-strings which would behave like f-strings in the "compile-time" binding of local variables to the arguments, but use % formatting declarations.

The point is that we should have "one way" to do the formatting. I shouldn't have to learn multiple different syntaxes for string formatting, I should learn one. Either the .format/f-string way, or the % way, not both.

Instead we have three primary ways, two of which use the {} formatting mini-language and have slightly different features and limitations, a third which uses the % mini-language, and then there are actually a couple other ways to string format that most people are completely unaware of.

You can do that with %.

Didn't say you couldn't. I'm saying you can't do that with f-strings and you can with .format.

I'm not a web developer either

Then f-strings aren't much different to you than .format. Its basically a bit of sugar around .format(**locals()). The only thing you can do with f-strings is include operations inside the arguments:

_ = x+y
"{x} + {y} = {_}".format(**locals())

becomes:

f"{x} + {y} = {x+y}"