Python 为什么我不能使用加星标的表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40676085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I use a starred expression?
提问by gboffi
My code
我的代码
$ python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = (1, 2)
>>> '%d %d %d' % (0, *a)
'0 1 2'
>>> '%d %d %d' % (*a, 3)
'1 2 3'
>>> '%d %d' % (*a)
File "<stdin>", line 1
SyntaxError: can't use starred expression here
>>>
My question, why?
我的问题,为什么?
In a more serious tone: I'd like an answer, or a reference, that details all the ins and outs of using a starred expression, as it happens that I am sometimes surprised from its behaviours...
用更严肃的语气:我想要一个答案或参考,详细说明使用加星标的表达式的所有来龙去脉,因为碰巧我有时会对它的行为感到惊讶......
Addendum
附录
To reflect some of the enlightening comments that immediately followed my question I add the following code
为了反映我的问题后立即出现的一些启发性评论,我添加了以下代码
>>> '%d %d' % (, *a)
File "<stdin>", line 1
'%d %d' % (, *a)
^
SyntaxError: invalid syntax
>>> '%d %d' % (*a,)
'1 2'
>>>
(I had tried the (, a)
part before posting the original question but I've omitted it 'cause the error was not related to the starring.)
(我(, a)
在发布原始问题之前尝试过该部分,但我省略了它,因为错误与主演无关。)
There is a syntax, in python ≥ 3.5, that "just works" but nevertheless I would like some understanding.
在 python ≥ 3.5 中有一种语法“可以正常工作”,但我还是希望得到一些理解。
回答by B?a?ej Michalik
It's because this:
这是因为:
(a)
Is just a value surrounded by parenthesis. It's not a new tuple object. So your expression:
只是一个被括号包围的值。它不是一个新的元组对象。所以你的表达:
>>> '%d %d' % (*a)
will get translated to:
将被翻译成:
>>> '%d %d' % * a
which is obviously wrong in terms of python syntax.
这在 python 语法方面显然是错误的。
In order to create a new tuple, with one expression as an initializer, you need to add a ',
' after it:
为了创建一个新的元组,用一个表达式作为初始化器,你需要,
在它后面添加一个“ ”:
>>> '%d %d' % (*a,)
Note: unless a
is a generator, in this particular situation you could just type:
注意:除非a
是生成器,在这种特殊情况下,您只需键入:
>>> '%d %d' % a
Also, if I may suggest something: you could start using new-style formating expressions. They are great!
另外,如果我可以提出一些建议:您可以开始使用新样式的格式化表达式。他们都是伟大的!
>>> "{} {}".format(*a)
You can read more about them in those twoparagraphs of python documentation, also there is thisgreat website. The line above uses argument unpacking mechanism described below.
你可以阅读更多关于他们在那些牛逼w ^ ōPython文档中的段落,也有这个伟大的网站。上面的行使用下面描述的参数解包机制。
Starred Expressions
加星标的表达式
There are many more uses to starred expression than just creating a new list/tuple/dictionary. Most of them are described in this PEP, and this one
带星号的表达式除了创建新的列表/元组/字典之外,还有更多用途。他们中的大多数都在这个 PEP 中进行了描述,而这个
All of them come down to two kinds:
所有这些都归结为两种:
RValue unpacking:
RValue 解包:
>>> a, *b, c = range(5)
# a = 0
# b = [1, 2, 3]
# c = 4
>>> 10, *range(2)
(10, 0, 1)
Iterable / dictionary object initialization (notice that you can unpack dictionaries inside lists too!):
可迭代/字典对象初始化(请注意,您也可以解压缩列表中的字典!):
>>> [1, 2, *[3, 4], *[5], *(6, 7)]
[1, 2, 3, 4, 5, 6, 7]
>>> (1, *[2, 3], *{"a": 1})
(1, 2, 3, 'a')
>>> {"a": 1, **{"b": 2, "c": 3}, **{"c": "new 3", "d": 4}}
{'a': 1, 'b': 2, 'c': 'new 3', 'd': 4}
Of course, the most often seen use is arguments unpacking:
当然,最常见的用法是参数解包:
positional_arguments = [12, "a string", (1, 2, 3), other_object]
keyword_arguments = {"hostname": "localhost", "port": 8080}
send(*positional_arguments, **keyword_arguments)
which would translate to this:
这将转化为:
send(12, "a string", (1, 2, 3), other_object, hostname="localhost", port=8080)
This topic was already covered in great extent in another Stack Overflow question.
回答by Marcus Müller
My question, why?
我的问题,为什么?
Because your python syntax doesn't allow that. It's defined that way, so there's no real "why".
因为您的 python 语法不允许这样做。它是这样定义的,所以没有真正的“为什么”。
also, it's unnecessary.
而且,也没有必要。
"%d %d" % a
would work.
会工作。
So, you'd need to convert your expansion to a tuple – and the right way of doing that would be, as pointed out by Lafexlos, be
因此,您需要将扩展转换为元组 - 正如 Lafexlos 所指出的那样,正确的做法是
"%d %d" % (*a,)
回答by U10-Forward
It's because:
这是因为:
>>> '%d %d' % (*a)
Can be just:
可以只是:
>>> '%d %d' %a
Of course then able to do:
当然然后能够做到:
>>> '%d %d' % (*a,)
But then:
但是之后:
>>> (*a,)==a
True
>>>
Or you can do:
或者你可以这样做:
>>> '%d %d' % [*a]
But then:
但是之后:
>>> [*a]
[1, 2]
>>> a
(1, 2)
>>>
So:
所以:
>>> tuple([*a])==a
True