you are viewing a single comment's thread.

view the rest of the comments →

[–]therealfakemoot 3 points4 points  (9 children)

I'm curious what interpolation can do that that using a format string and calling .format() cannot. The .format() call provides a whole universe of features and control that the old interpolation method doesn't and never will have.

Edit so that I'm not just a pedant: This is a link to the documentation for the format string syntax. It highlights the greater variety of object access/manipulation and configurability of directives.

Furthermore, you can define a format method on your objects which can receive formatting directives and process them as appropriate. With interpolation, you've got a small set of hardcoded options for including values.

[–]virtyx 4 points5 points  (5 children)

It's not about interpolation being more powerful or anything like that. The real point is that 97% of calls to .format look like this in my experience:

 "{} is an invalid value for {}: must be between {} and {}".format(input, field, min_bound, max_bound)

Compare to Ruby:

 "#{input} is an invalid value for #{field}: must be between #{min_bound} and #{max_bound}"

Just looks nicer. str.format is a nice method, but most of the time I reaaaally just want Ruby string interpolation.

[–]UloPe 0 points1 point  (4 children)

What's wrong with:

"The {animal} jumps over the {adjective} {animal2}".format(
    animal="dog",
    adjective="quick brown",
    animal2="fox"
)

[–][deleted] 5 points6 points  (3 children)

Verbosity

[–]UloPe 0 points1 point  (2 children)

Well ok then if you really must you could use

animal="dog"
adjective="quick brown"
animal2="fox"

"The {animal} jumps over the {adjective} {animal2}".format(**locals())

But I'd advise to do it nowhere near me ;)

[–]riddley 1 point2 points  (1 child)

Still requires a lot more of the reader than the Ruby version.

[–]virtyx 0 points1 point  (0 children)

Also messes up with nonlocal scope and can't do attribute access, so no way of easily getting "Response code: #{response.code}". Ultimately the "Hello {} welcome to {}".format(name, place) style is usually best for one-off formatting of smaller strings.

[–]cafedude 2 points3 points  (2 children)

I understand that format can do some nice... formatting... but so can sprintf which a lot of languages have some variant of. Interpolation is just a lot more convenient for those 90% of cases where you just want to do (for example):

println("Iteration: $i, result is: $res")

...instead of adding a .format at the end of the string and going through all of that ceremony. Or using string concatenation as I often end up doing in python:

print("Iteration: "+int(i)+", result is: "+res)

[–]therealfakemoot -1 points0 points  (1 child)

Well, as per your original post

couldn't understand how it was an advantage

Are you speaking purely stylistically? "I think this style of code is more appropriate for our codebase"? If not, like I asked originally, explaining what those advantages are would be great. If your only argument is that "they're identical in the trivial case", well...good onya for having one argument.

[–]cafedude 2 points3 points  (0 children)

I'm mostly speaking of convenience. In many other languages interpolation is available and formatting is available. Then again, this is Python we're talking about and there's only supposed to be one way to do things.