Python tuple vs generator

前端 未结 2 1430
醉梦人生
醉梦人生 2021-02-07 11:08

I am having a problem understanding why one of the following line returns generator and another tuple.

How exactly and why a generator is created in the second line, wh

2条回答
  •  粉色の甜心
    2021-02-07 11:34

    Parentheses are used for three different things: grouping, tuple literals, and function calls. Compare (1 + 2) (an integer) and (1, 2) (a tuple). In the generator assignment, the parentheses are for grouping; in the tuple assignment, the parentheses are a tuple literal. Parentheses represent a tuple literal when they contain a comma and are not used for a function call.

提交回复
热议问题