6 ms·
This is certainly interesting, but I think it kind of breaks with the "only one obvious way to do things," which Ruby tends not to follow (and with regards to s
by rpcope1 12y ago
This is certainly interesting, but I think it kind of breaks with the "only one obvious way to do things," which Ruby tends not to follow (and with regards to string formatting, Python doesn't either :P ). Does this buy you anything beyond trying to ramrod Ruby syntax into Python? If this truly compiles down to byte code that's doing string concatenation in the Python VM, the built-in string formatting library tends to be a non-trivial amount faster, beyond trying to write Ruby in a language that isn't Ruby.
- renox 12y ago> Does this buy you anything beyond trying to ramrod Ruby syntax into Python? I'm not the poster but IMHO it fixes Python's syntax which is a case of DRY violation.
- rpcope1 12y agoMaybe you can elucidate what about Python's syntax is broken or not DRY? EDIT: To me, seeing this is Python code anywhere would seem to violate principle of least astonishment, which I think is somewhat more important than being DRY, if that's even a problem here.
- philh 12y agoIf you want format strings with sensible placeholders, you end up with something like "Hello {person}, it's a {weather} day".format(person=person, weather=weather) The list of variables (person, weather) shows up three times. Somewhat off-topic, a similar problem shows up when you have a bunch of related functions, all taking a particular kwarg (or kwargs), and calling each other. Like def foo(arg, conf1=None, conf2=None): bar(arg+2, conf1=conf1, conf2=conf2) # eww :( def bar(arg, conf1=None, conf2=None): # etc. A neat thing that perl6 has is syntax for "keyword argument whose value is found in the variable of the same name". So the equivalent of that line could be written bar($arg+2, :conf1($conf1), :conf2($conf2)) but it could also be bar($arg+2, :$conf1, :$conf2)
- methodover 12y agoWell... String formatting already sorta breaks the "only one obvious way to do something" rule. I dunno about you, but we have string formatting all over the place in our code. I would wager that every single module in our codebase has something that looks like this: cache.add_message("Hey, your {zoop} is {boop}".format(zoop=zoop, boop=boop)) That that call to "format" just kind of sucks -- it repeats what's already pretty obvious by looking at the string. It's specially crappy when you have strings that need lots of variables. We've taken to doing this recently, which I'm usually okay with: cache.add_message("Hey, your {} is {}".format(zoop, boop)) The downside of that is you have to make sure that the order of the arguments matches exactly with the order of the empty brackets. It's kinda error prone... But generally not that big of a deal. We could also do this... cache.add_message("Hey, your %s is %s" % zoop, boop)) Or this... cache.add_message("{1} alert! Hey, your {0} is {1}".format(zoop, boop)) So yeah, I would argue that string formatting in Python is ALREADY in a kinda nasty place. There's ALREADY a bunch of ways to do it, and it all just kinda sucks. IMO, Ruby-style string formatting is probably the nicest I've seen. If it were in Python, it absolutely would be THE way to do string formatting, I bet. cache.add_message("Hey, your {zoop} is {boop}") So much nicer.
- dragonwriter 12y agoActually, I kind of like the existing Python ways better in many respects from the Ruby one, since it lets you separate format strings from the parameters passed to them, and reuse format strings more easily (which is especially useful if you want to move string literals out of code and into resources, because then you can include format strings in that as well.)
- methodover 12y agoOh hey, I didn't even think of that. That's a really good point.
- stouset 12y agoYou can do this in Ruby too, with sprintf. It's almost as if there are multiple, complimentary ways of accomplishing similar tasks, depending on your needs.