为什么我不能用星号表示?

时间:2022-05-02 01:21:18

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,“作品”但我想要一些理解。

2 个解决方案

#1


10  

It's because this:

这是因为:

(a)

Is just a value surrounded by parenthesis. It's not a new tuple object. So your expression:

只是一个被括号括起来的值。它不是一个新的tuple对象。所以你的表达式:

>>> '%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:

为了创建一个新的Tuple,使用一个表达式作为初始化器,您需要添加一个“,”

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

在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

除了创建一个新的列表/tuple/dictionary外,还有很多其他的用法。他们中的大多数在这个PEP中描述,这个。

All of them come down to two kinds:

所有这些都归结为两种:

RValue 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!):

Iterable / dictionary对象初始化(注意,您也可以在列表中打开字典):

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

这个主题已经在另一个堆栈溢出问题中得到了很大的讨论。

#2


3  

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

#1


10  

It's because this:

这是因为:

(a)

Is just a value surrounded by parenthesis. It's not a new tuple object. So your expression:

只是一个被括号括起来的值。它不是一个新的tuple对象。所以你的表达式:

>>> '%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:

为了创建一个新的Tuple,使用一个表达式作为初始化器,您需要添加一个“,”

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

在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

除了创建一个新的列表/tuple/dictionary外,还有很多其他的用法。他们中的大多数在这个PEP中描述,这个。

All of them come down to two kinds:

所有这些都归结为两种:

RValue 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!):

Iterable / dictionary对象初始化(注意,您也可以在列表中打开字典):

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

这个主题已经在另一个堆栈溢出问题中得到了很大的讨论。

#2


3  

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