[Python-ideas] Draft PEP on string interpolation

Yury Selivanov yselivanov.ml at gmail.com
Sat Aug 22 01:35:46 CEST 2015


On 2015-08-21 6:59 PM, Nick Coghlan wrote:
> It's also worth reiterating my concept of using "!" to introducing the
> arbitrary "magic happens here" prefixes. That is, you'd write them
> like this:
>
>      myquery = !sql"SELECT $column FROM $table;"
>      mycommand = !sh"cat $filename"
>      mypage = !html"<html><body>$content</body></html>"

I too like the macros concept, especially how it's implemented
in Rust.  Your examples would look like:

      myquery = sql!"SELECT $column FROM $table;"
      mycommand = sh! "cat $filename"

and it'd be possible to do even more:

      v = vec! [1, 2, 3]
      debug!("error {error code}")

To implement macros we'll have to introduce another import
step -- macros expansion, during which Python would resolve
macros names and evaluate them, storing the transformation
result in pyc files and creating new (updated) code objects.

All in all, I don't think that all the extra complexity
required to have full macros support is worth it.  Template
Strings would be a great alternative, much easier to
implement.

Yury



More information about the Python-ideas mailing list