f-strings 应该在 Python 3.4 中工作吗?

Are f-strings supposed to work in Python 3.4?

这是否适用于 Python 3.4:

>>> a='tttt'

>>> print(f'The value of a is {a}')

否,f 字符串 were introduced in Python 3.6 目前(截至 2016 年 8 月)处于 alpha 阶段。

如果确实需要,您至少可以模仿它们

def f(string):
    # (!) Using globals is bad bad bad
    return string.format(**globals())

# Use as follows:
ans = 'SPAM'
print(f('we love {ans}'))

或者其他一些方式,比如 class 重新加载 __getitem__ 如果你喜欢 f[...] 语法

我写了一个(糟糕的)thing,通过编码技巧启用它们:

第一个:

pip install future-fstrings

然后替换编码 cookie(如果你有的话)然后砰! python<3.6

中的 f 弦
# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')

运行时间:

$ python2.7 main.py
hello world

我刚刚为 f-string 编写了一个后向端口编译器,为 JavaScript.

调用了 f2format. You may write f-string literals in Python 3.6 flavour, and compile to a compatible version for end-users to run, just like Babel

f2format 应将 f-string 文字替换为 str.format 方法,同时保持源代码的原始布局。你可以简单地使用

f2format /path/to/the/file_or_directory

这将重写所有 Python 文件。例如,

# original source code
var = f'foo{(1+2)*3:>5}bar{"a", "b"!r}boo'
# after f2format
var = ('foo{:>5}bar{!r}boo').format(((1+2)*3), ("a", "b"))

字符串连接、转换、格式规范、多行和 unicode 都得到正确处理。有关更多信息,请查看 README.

是的,如果安装和使用 future-fstrings 模块,可以使用 Python 3.x 低于 3.7 的 f-strings,但有一些重要的细微差别。

  1. "# -*- coding: future_fstrings -*-" 行必须是脚本的第一行。

  2. 井号#很重要,否则此语法将无效。

  3. future-fstrings 和 tokenize_rt 模块的版本必须合适,否则会出现奇怪的错误信息。我将 post 在这里提供有关此问题的更多详细信息,因为我看到很多人都在谷歌搜索这些错误消息。

简单脚本:

test_fstrings_A.py:
import test_fstrings_B

test_fstrings_B.py:
# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')

输出:

$ python3 test_fstrings_B.py
  File "test_fstrings_B.py", line 1
SyntaxError: encoding problem: future_fstrings

$ python3 test_fstrings_A.py
Traceback (most recent call last):
  File "test_fstrings_A.py", line 2, in <module>
    import test_fstrings_B
  File "/home/build01/test_fstrings_B.py", line 0
SyntaxError: invalid syntax (tokenize_rt.py, line 22)

我遇到了这个麻烦,因为我的系统上提到的模块版本有误:

$ cd $HOME/.local/lib/python3.5/site-packages; ls -d *.dist-info; cd -
future_fstrings-1.2.0.dist-info  pip-20.3.1.dist-info  tokenize_rt-4.0.0.dist-info

为了检查版本是否正确,可以先将模块作为 .whl 安装在本地目录中,然后按照我从这个问题中了解到的方式进行测试 https://bugs.python.org/issue33944#msg333705

$ python3 -m pip download -d pkgs future_fstrings
...
Saved ./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl
Saved ./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl
Successfully downloaded tokenize-rt future-fstrings

$ PYTHONPATH=./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl:./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl python3 test_fstrings_A.py
hello world
$ PYTHONPATH=./pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl:./pkgs/tokenize_rt-3.2.0-py2.py3-none-any.whl python3 test_fstrings_B.py
hello world

换句话说,命令“python3 -m pip download”安装并输出正确的(适用于安装的Python版本)版本的模块。 这帮助我看到版本 tokenize_rt-4.0.0 在我的 Python 安装位置是错误的。

最后一步是全局修复它,使用 pip3 的 --force-reinstall 参数:

$ pip3 install --force-reinstall pkgs/future_fstrings-1.2.0-py2.py3-none-any.whl
or
$ python3 -m pip install --force-reinstall future_fstrings

更新:在某些情况下,命令“python3 -m pip download”无论如何都会安装错误的 tokenize_rt 4.0.0 版本。在这种情况下,只有明确的版本说明有帮助:

$ python3 -m pip install --force-reinstall future_fstrings==1.2.0 tokenize_rt==3.2.0