Added some examples from Python's source. Both 'before' (str.format) and 'after' (f-strings).

This commit is contained in:
Eric V. Smith 2015-08-24 15:49:18 -04:00
parent 03ef9d23e5
commit 08777faecd
1 changed files with 29 additions and 0 deletions

View File

@ -551,6 +551,35 @@ If you feel you must use lambdas, they may be used inside of parens::
>>> f'{(lambda x: x*2)(3)}'
'6'
Examples from Python's source code
==================================
Here are some examples from Python source code that currently use
str.format(), and how they would look with f-strings. This PEP does
not recommend wholesale converting to f-strings, these are just
examples of real-world usages of str.format() and how they'd look if
written from scratch using f-strings.
Lib/asyncio/locks.py::
extra = '{},waiters:{}'.format(extra, len(self._waiters))
extra = f'{extra},waiters:{len(self._waiters)}'
Lib/configparser.py::
message.append(" [line {0:2d}]".format(lineno))
message.append(f" [line {lineno:2d}]")
Tools/clinic/clinic.py::
methoddef_name = "{}_METHODDEF".format(c_basename.upper())
methoddef_name = f"{c_basename.upper()}_METHODDEF"
python-config.py::
print("Usage: {0} [{1}]".format(sys.argv[0], '|'.join('--'+opt for opt in valid_opts)), file=sys.stderr)
print(f"Usage: {sys.argv[0]} [{'|'.join('--'+opt for opt in valid_opts)}]", file=sys.stderr)
References
==========