you can already put an arbitrary expression in a f-string -- that's the point. So what's wrong with:
In [12]: eget = os.environ.get In [13]: f"{eget('HOME')}/bin" Out[13]: '/Users/chris/bin' This seems a rare enough need that special built-in support is not worth it. -CHB On Fri, Feb 11, 2022 at 6:01 PM Eric V. Smith <e...@trueblade.com> wrote: > On 2/11/2022 5:33 PM, Ricky Teachey wrote: > > On Fri, Feb 11, 2022 at 4:58 PM MRAB <pyt...@mrabarnett.plus.com> wrote: > >> On 2022-02-11 20:05, Ricky Teachey wrote: >> > Just had a thought kernel: what if there were an f-string mini-language >> > directive to grab environment variables and expand user paths? That >> > seems to me like it could be even more useful beyond just working with >> > paths. >> > >> > Maybe something like: >> > >> > f"{my_var:$}" >> > >> > This would return the same as os.path.expandvars("$my_var") >> > >> No, that would be the value of the Python variable 'my_var' with the >> format string "$". >> > > well right now $ is not a valid format string for f-strings. > > Off topic: there are no characters that are invalid format strings, except > '!' and ':'. For example, datetime allows anything, since it's passed on to > strftime: > > >>> from datetime import date > >>> f'{datetime.now():This is a test $@$&*()}' > 'This is a test $@$&*()' > > Or: > > >>> class F: > ... def __format__(self, fmt): > ... if fmt == '$': > ... return 'dollar' > ... return 'something else' > ... > >>> f'{F():$}' > 'dollar' > >>> f'{F():x}' > 'something else' > > Eric > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/JGZAU2A7EQIMTOWPTMTNRLRIBDTK4BJX/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- Christopher Barker, PhD (Chris) Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DNNTUWCFGRBAXRTTO6PGYXDHCDONG474/ Code of Conduct: http://python.org/psf/codeofconduct/