[Python-ideas] String interpolation: environment variables, command substitution
Eric V. Smith
eric at trueblade.com
Wed Aug 26 21:21:11 CEST 2015
On 08/26/2015 02:20 PM, Mike Miller wrote:
> One of the remaining questions for the string interpolation subject is
> whether to allow for easy access to environment variables and
> output-capture of external processes (aka command-substitution) as bash
> does.
>
> While incredibly useful in use-cases such as shell-script replacements,
> the functionality is perceived to be, if not dangerous. More so than
> arbitrary expressions? Given that we are talking about string literals
> and not input, I'm not sure, so am looking for feedback.
>
> The idea is not unheard of in Python, there was a module that captured
> process output called commands in the old days, which was superseded at
> some point by subprocess.check_output() I believe.
>
> Here is some example syntax modeled on bash, though placed inside
> .format braces. Note both start with $ as the signal::
>
> >>> x'Home folder: {$HOME}' # environment
> 'Home folder: /home/nobody'
>
> >>> x'Files: {$(/bin/ls .)}' # capture output
> 'foo foo1 foo2'
-1000 for any language syntax that allows access to environment
variables or shell output.
That said, with PEP-498 you can do:
>>> import os
>>> f'HOME={os.environ["HOME"]}'
'HOME=/home/eric'
Which is about as easy as I'd like to make this.
Eric.
More information about the Python-ideas
mailing list