3 ms·
Note that you can do this in old version of Python, too, using the "format-locals" idiom: some_var = ... "{some_var}".format(**locals()) Although thi
by vog 9y ago
Note that you can do this in old version of Python, too, using the "format-locals" idiom:
some_var = ...
"{some_var}".format(**locals())
Although this does have some limitations regarding variables to strictly being in the local scope, this is a fine idiom if you need to be compatible with older Python versions.
- cyphar 9y agoThat is a bit of a hack though ;). You could also do it with %-style formatting as well: some_var = ... "%(some_var)s" % locals() If you want to do both locals and globals you can do: some_var = ... "%(some_var)s" % dict(globals(), **locals()) New Python versions (>=3.5) have this syntax: "%(some_var)s" % {**globals(), **locals()} But those versions also have f-strings so that's sort of moot.