pep448

Python: Splat/unpack operator * in python cannot be used in an expression?

て烟熏妆下的殇ゞ 提交于 2019-12-28 04:22:08
问题 Does anybody know the reasoning as to why the unary ( * ) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File "<stdin>", line 1 [1,2,3, *[4,5,6]] ^ SyntaxError: invalid syntax Why doesn't the * operator: [1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6] whereas when the * operator is used with a function call it does expand: f(*[4, 5, 6]) is equivalent to f

asterisk in tuple, list and set definitions, double asterisk in dict definition

好久不见. 提交于 2019-12-17 12:36:23
问题 I'm playing now with Python 3.5 interpreter and found very interesting behavior: >>> (1,2,3,"a",*("oi", "oi")*3) (1, 2, 3, 'a', 'oi', 'oi', 'oi', 'oi', 'oi', 'oi') >>> [1,2,3,"a",*range(10)] [1, 2, 3, 'a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ('aw','aw',*range(10),*(x**2 for x in range(10))) ('aw', 'aw', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81) >>> {"trali":"vali", **dict(q=1,p=2)} {'q': 1, 'p': 2, 'trali': 'vali'} >>> {"a",1,11,*range(5)} {0, 1, 2, 3, 4, 11, 'a'} I have

Python: Splat/unpack operator * in python cannot be used in an expression?

十年热恋 提交于 2019-11-28 00:39:29
Does anybody know the reasoning as to why the unary ( * ) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File "<stdin>", line 1 [1,2,3, *[4,5,6]] ^ SyntaxError: invalid syntax Why doesn't the * operator: [1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6] whereas when the * operator is used with a function call it does expand: f(*[4, 5, 6]) is equivalent to f(4, 5, 6) There is a similarity between the + and the * when using lists but not when extending a list

asterisk in tuple, list and set definitions, double asterisk in dict definition

主宰稳场 提交于 2019-11-27 14:49:32
I'm playing now with Python 3.5 interpreter and found very interesting behavior: >>> (1,2,3,"a",*("oi", "oi")*3) (1, 2, 3, 'a', 'oi', 'oi', 'oi', 'oi', 'oi', 'oi') >>> [1,2,3,"a",*range(10)] [1, 2, 3, 'a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ('aw','aw',*range(10),*(x**2 for x in range(10))) ('aw', 'aw', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81) >>> {"trali":"vali", **dict(q=1,p=2)} {'q': 1, 'p': 2, 'trali': 'vali'} >>> {"a",1,11,*range(5)} {0, 1, 2, 3, 4, 11, 'a'} I have never seen that neither in documentation and examples nor in any source code despite several years of