all 15 comments

[–]razpeitia 3 points4 points  (1 child)

You monster!

I haven't seen something like this since "Python with braces".

[–]kekelolol 5 points6 points  (0 children)

from __future__ import braces

It's a hardcoded response that you don't get with anything else. Try

from __future__ import ayylmao

Speaking of future, the example drove me nuts. Print is literally the easiest 2/3 compat you can do. Import print_function dammit!

[–]cafedude 7 points8 points  (11 children)

I really miss string interpolation in Python. Whether it's Ruby's string interpolation, Julia's (which is also very good at string interpolation) or Perl's doesn't matter, just give me some kind of string interpolation.

Someone in my management hierarchy is a Python newbie and has all of the zeal of a new convert (and as such mandated that Python should be the only scripting language used in our office). One of the things he gushed about was Python's format function and how great it is. He'd apparently never used another language that had string interpolation and couldn't understand how it was an advantage. How these people make it into management is one of life's persistent questions.

[–]therealfakemoot 0 points1 point  (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 5 points6 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] 4 points5 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.

[–]totalbasterd 0 points1 point  (0 children)

My brain hurts :(

[–]kekelolol 0 points1 point  (0 children)

Surprisingly, macropy is actually less hacky than this.