为什么我不能使用加星标的表达式? [英] Why can't I use a starred expression?

查看:59
本文介绍了为什么我不能使用加星标的表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

$ pythonPython 3.5.2 |Continuum Analytics, Inc.|(默认,2016 年 7 月 2 日,17:53:06)[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] 在 linux 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>a = (1, 2)>>>'%d %d %d' % (0, *a)'0 1 2'>>>'%d %d %d' % (*a, 3)'1 2 3'>>>'%d %d' % (*a)文件<stdin>",第 1 行语法错误:此处不能使用带星号的表达式>>>

我的问题,为什么?

用更严肃的语气:我想要一个答案或参考,详细说明使用加星标的表达式的所有来龙去脉,因为碰巧我有时会对它的行为感到惊讶......

附录

为了反映一些启发性的评论,立即按照我的问题添加以下代码

<预><代码>>>>'%d %d' % (, *a)文件<stdin>",第 1 行'%d %d' % (, *a)^语法错误:无效语法>>>'%d %d' % (*a,)'1 2'>>>

(我在发布原始问题之前尝试了 (, a) 部分,但我已经省略了它,因为错误与主演无关.)

python ≥ 3.5 中有一种语法可以正常工作",但我还是想了解一下.

解决方案

这是因为:

(a)

只是一个用括号括起来的值.它不是一个新的元组对象.所以你的表情:

<预><代码>>>>'%d %d' % (*a)

将被翻译成:

<预><代码>>>>'%d %d' % * a

这在python语法方面显然是错误的.

为了创建一个新的元组,用一个表达式作为初始化器,你需要在它后面添加一个',':

<预><代码>>>>'%d %d' % (*a,)

注意:除非 a 是一个生成器,在这种特殊情况下你可以只输入:

<预><代码>>>>'%d %d' % a

另外,如果我可以提出一些建议:您可以开始使用新样式的格式化表达式.他们很棒!

<预><代码>>>>"{} {}".format(*a)

您可以在那些 two 段python 文档,还有这个 很棒的网站.上面的行使用了下面描述的参数解包机制.

更新:从 python 3.6 开始,您还可以使用字符串插值 - f-strings!这些在 PEP-498 中有描述,一些示例可以在 Python 文档.

加星标的表达式

带星号的表达式除了创建新的列表/元组/字典之外,还有更多用途.其中大部分都在本 PEP 中进行了描述,并且这个

所有这些都归结为两种:

R 值解包:

<预><代码>>>>a, *b, c = 范围(5)# a = 0# b = [1, 2, 3]# c = 4>>>10、*范围(2)(10, 0, 1)

可迭代/字典对象初始化(请注意,您也可以解压缩列表中的字典!):

<预><代码>>>>[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":新 3",d":4}}{'a':1,'b':2,'c':'新 3','d':4}

当然,最常见的用法是参数解包:

positional_arguments = [12, 一个字符串", (1, 2, 3), other_object]关键字参数 = {主机名":本地主机",端口":8080}发送(*positional_arguments,**keyword_arguments)

这将转化为:

send(12, "a string", (1, 2, 3), other_object, hostname="localhost", port=8080)

此主题已在另一个堆栈溢出问题中大量讨论.

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.)

There is a syntax, in python ≥ 3.5, that "just works" but nevertheless I would like some understanding.

解决方案

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.

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:

>>> '%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 two paragraphs of python documentation, also there is this great website. The line above uses argument unpacking mechanism described below.

Update: since python 3.6, you could also use string interpolation - f-strings! These are described in PEP-498, and some examples can be found in Python documentation.

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

All of them come down to two kinds:

R-value unpacking:

>>> 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 has already been covered to a substantial extent in another Stack Overflow question.

这篇关于为什么我不能使用加星标的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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