On Sat, 12 Feb 2022 at 09:34, Ricky Teachey <ri...@teachey.org> 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. what i'm > suggesting is to add $ to the format spec mini-languagem and have the result > be an expanded environment variable value. > > but you're right, i guess it would apply this to the contents of the variable > my_var, not the STRING my_var. so maybe the spelling would need to use a > nested quotation, like this: > > f"{'my_var':$}" > > this would be the same as os.path.expandvars("$my_var") >
Why shoehorn it into f-strings? Even worse: Why should f-strings fetch external information in such a hidden way? One thing I'm not fully aware of here is what precisely is wrong with the status quo. Are you looking for a method instead of a function? Do you need something that takes a Path and returns a Path? The existing os.path.expandvars will happily accept a Path and return a str: >>> import pathlib, os.path >>> pathlib.Path("$HOME/cpython/Grammar/python.gram") PosixPath('$HOME/cpython/Grammar/python.gram') >>> os.path.expandvars(pathlib.Path("$HOME/cpython/Grammar/python.gram")) '/home/rosuav/cpython/Grammar/python.gram' >>> type(_) <class 'str'> I'm confused, since anything involving f-strings clearly has nothing to do with pathlib (other than in the trivial sense that you could expand vars before constructing a Path), but the OP was very definitely talking about Path objects. ChrisA _______________________________________________ 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/J4Y335LUOAC2MT74BQT7YLRJVU4CBTLX/ Code of Conduct: http://python.org/psf/codeofconduct/