f字符串中的星号(星号)做什么? [英] What does a star (asterisk) do in f-string?

查看:179
本文介绍了f字符串中的星号(星号)做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python文档中 2.4.3.格式化的字符串文字,似乎可以在f字符串的 {} 中写一个星号后跟一个表达式,但是我找不到如何使用它.

In the python document 2.4.3. Formatted string literals, it seems possible to write a star followed by an expression in a f-string's {}, but I cannot find how to use that.

那是什么以及我如何使用它?它记录在某处吗?

What's that and how I can use it? Is it documented somewhere?

确切地说,这与以下"*" or_expr 部分有关rel ="noreferrer"> BNF .

To be exact, this is regarding "*" or_expr part of the following BNF.

f_string          ::=  (literal_char | "{{" | "}}" | replacement_field)*
replacement_field ::=  "{" f_expression ["!" conversion] [":" format_spec] "}"
f_expression      ::=  (conditional_expression | "*" or_expr)
                         ("," conditional_expression | "," "*" or_expr)* [","]
                       | yield_expression

我在REPL中尝试过,但是会导致错误.

I tried it in REPL, but it causes an error.

>>> l = [1, 2, 3]
>>> f"{l}"
'[1, 2, 3]'
>>> f"{*l}"
  File "<stdin>", line 1
SyntaxError: can't use starred expression here

推荐答案

f_expression 有两种选择:用逗号分隔的 or_expression s列表,可选地以星号开头,或单个 yield_expression .请注意, yield_expression 不允许使用星号.

There are two alternatives for f_expression: a comma separated list of or_expressions, optionally preceded by asterisks, or a single yield_expression. Note the yield_expression does not allow an asterisk.

我认为这样做的目的是仅在至少有一个逗号的情况下才选择用逗号分隔的列表,但是语法实际上并没有这样说.我觉得最后的重复运算符应该是 + 而不是 * .

I assume the intention was that the comma-separated list alternative is only chosen when there's at least one comma, but the grammar doesn't actually say that. I feel like the repetition operator at the end should have been a + instead of a *.

因此 f"{* 1}" 将是语法错误,因为其中有一个星号,但没有逗号. f"{* 1,* 2}" 在语法上有效,但是由于1和2不可迭代而导致类型错误. f"{* [1],* [2]}" 有效,并且与 f"{1,2}" 相同.因此允许使用星号,因为它在元组中充当splat运算符,可以在f表达式中不加括号地编写该星号.

So f"{*1}" would be a syntax error because there's an asterisk, but no comma. f"{*1,*2}" is syntactically valid, but a type error because 1 and 2 aren't iterable. f"{*[1], *[2]}" is valid and acts the same as f"{1,2}". So the asterisk is allowed because it acts as the splat operator in tuples, which can be written without parentheses in f-expressions.

请注意,使用 or_expr 作为 * 的操作数并不意味着必须在其中使用按位or运算符-只是意味着按位or运算符是Priority-hierachy中的第一个运算符,该运算符将被允许作为 * 的操作数.因此,这只是设置前缀 * 与其他表达式的优先级.我相信 or_expression 始终被用作在语法中的任何地方(在前缀 * 后跟表达式的地方)加 * 前缀的操作数.而不是参数名称.

Note that using or_expr as the operand to * does not mean that a bitwise or-operator has to be used there - it just means that the bitwise or-operator is the first operator in the precedence-hierachy that would be allowed as an operand to *. So it's just about setting the precedence of prefix * vs. other expressions. I believe or_expression is consistently used as the operand to prefix * everywhere in the grammar (that is, everywhere where prefix * is followed by an expression as opposed to a parameter name).

这篇关于f字符串中的星号(星号)做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆