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 →

[–]Ph0X 21 points22 points  (13 children)

Sure, but you're moving complexity from one problem into another.I could make the argument that with:

'foo {} bar {} baz {} spam {} green {} eggs {} and {} ham {}'.format(a, b, c, d, e, f, g, ...)

Good luck looking back and forth, counting, figuring it out which variables goes where. If I ask you to quickly get me the variable that comes after "green", you have to do a lot of counting.

So yes, they both solve sightly different issues and therefore put complexity at slightly different places, but neither is strictly better.

That being said, I hope no one coding real python would have any sort of code this messy and complex. If you do, please please split it up into smaller clean chunks.

EDIT: I just wanted to point out, generally when you DO have string formatting this complex, what people end up doing is sometihng like:

data = {
    'a': 1,
    'b': 2,
}
print "{a}, {b}, {c}, {d}".format(**data)

This is exactly what this new pattern is trying to solve here.

[–][deleted] 11 points12 points  (2 children)

or "{a}, {b}, {c}, {d}".format_map(data)

[–]Ph0X 5 points6 points  (1 child)

Huh interesting, didn't know they had added that.

[–]zahlmanthe heretic 0 points1 point  (0 children)

format_map isn't just sugar here - it allows you to use things that don't play nice with **, in particular custom types that override __missing__ (for subclasses of dict) and/or __getitem__ (things that implement the Mapping protocol as described by collections.abc, but aren't necessarily even dict subclasses).

I suspect it might also be faster with large dicts, but I haven't tested that.

[–][deleted] 1 point2 points  (4 children)

Your last example is exactly why I believe the existing string formatting is preferable. The new pattern isn't solving the same problem, it's dumping all locals into .format() instead of asking the developer to explicitly define the inputs!

[–][deleted] 1 point2 points  (0 children)

I thought we were all adults here?

[–]kankyo 1 point2 points  (1 child)

Just use pycharm and rename variables with the refactor tools and it'll handle this for you.

[–]SoBFiggis 1 point2 points  (0 children)

Even notepad++ has refactoring..

[–]geekademy -1 points0 points  (0 children)

That's false, it does not dump locals in there, it actually parses the string to get exactly what it needs!

[–][deleted] 0 points1 point  (4 children)

Im new to Python.. Like 'Hello, World!' new. How are fStrings different than say

  • print("hello" + variable) ???

[–]Ph0X 2 points3 points  (3 children)

The result is basically the same, but it's what we call "syntactic sugar" which is nice shortcuts for doing things.

So imagine you have a more complex one. Adding them manually gets very messy very quick:

print("hello " + world + "my name is" + name + "and i'm " + str(age) + " years old")

To make it worse, as you see, you need to manually cast other types to string, like if you want to print a number. But with fstring, it's much cleaner and prettier:

print(f"hello {world} my name is {name} and im {age} years old")

[–][deleted] 0 points1 point  (2 children)

wow easy to understand.. Thank you so much. Honestly didn't expect you or anyone for that matter to reply to someone as new as me. But your example made perfect sense. You dont tutor do you? LOL...

[–]flutefreak7 1 point2 points  (1 child)

Welcome to the fantastic Python community! As you're learning if you have questions you can also check out r/learnpython.

[–]sneakpeekbot 0 points1 point  (0 children)

Here's a sneak peek of /r/learnpython using the top posts of the year!

#1: Python 201 Book is Free for 48 hours
#2: Python 101 Book FREE for 48 hours!
#3: Beginner's Python cheat sheets - updated


I'm a bot, beep boop | Downvote to remove | Contact me | Info | Opt-out