Added some examples from Python's source. Both 'before' (str.format) and 'after' (f-strings).
This commit is contained in:
parent
03ef9d23e5
commit
08777faecd
29
pep-0498.txt
29
pep-0498.txt
|
@ -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
|
||||
==========
|
||||
|
||||
|
|
Loading…
Reference in New Issue