On Mon, Apr 2, 2012 at 5:24 PM, Yingjie Lan <lany...@yahoo.com> wrote: > They are called "Dynamic strings". > Dynamic strings can achieve formatting, > but the mechanism under the hood differ > from common strings dramatically. > > Many here didn't realize that this is not > another formatting proposal, it is a new > kind of *expression*. > > To have it in Python, we will need > a new kind of syntax to distinguish it > from other strings, such as raw strings > and the like. A raw string looks like: > >>>> r'my\\ raw str' > 'my\\\\ raw str' > > A dynamic string may look like this: > >>>> name = "Peter" #common string >>>> d"Thank you, $name$!" #dynamic string! > 'Thank you, Peter!'
>From the Zen of Python: Special cases aren't special enough to break the rules. It's fairly cheap to propose a new function. If people don't like it, they can ignore it... it can get stuck into a module somewhere and be easily ignored. If people DO like it, they can back-port it to older versions by simply copying and pasting the Python code (or "equivalent Python code", if the main version's written in C). New syntax like this has to be implemented in the parser, is (practically) impossible to back-port with pure Python, and has far higher potential to break existing code. The onus is on you to demonstrate that this syntax is worth this hassle. I'm -1 on the idea, mainly because there are already two perfectly good string interpolation syntaxes that don't require a new kind of string. I'm also against the notion of "interpolated strings" vs "non-interpolated strings" in general, having worked with them in PHP and not found any real benefit. But if you want it, you could fairly easily do something with an explicit interpolation parsing function: >>> d"Thank you, $name$!" #dynamic string! 'Thank you, Peter!' becomes: >>> d("Thank you, $name$!") #dynamic string! 'Thank you, Peter!' To make the magic of evaluation scope work, though, you'd need to fiddle around with tracebacks. It'd be far simpler and clearer to pass locals() and/or globals() to your function, if indeed you want that. Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list