*
反復子/リスト/タプルを含む式で単項 ( ) 演算子を使用できない理由を知っている人はいますか?
関数のアンパックのみに限定されるのはなぜですか? それとも私はそれを考えるのが間違っていますか?
例えば:
>>> [1,2,3, *[4,5,6]]
File "<stdin>", line 1
[1,2,3, *[4,5,6]]
^
SyntaxError: invalid syntax
*
オペレーターがしない理由:
[1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6]
一方、*
演算子を関数呼び出しで使用すると、次のように展開されます。
f(*[4, 5, 6]) is equivalent to f(4, 5, 6)
+
リストを使用する場合はと の間に類似点があり*
ますが、別のタイプでリストを拡張する場合は異なります。
例えば:
# This works
gen = (x for x in range(10))
def hello(*args):
print args
hello(*gen)
# but this does not work
[] + gen
TypeError: can only concatenate list (not "generator") to list