What does this syntax mean in Python?

前端 未结 2 564
说谎
说谎 2021-01-22 02:28

What does the comma in the declaration below mean? Does it define two variables at once?

resp, content = client.request(request_token_url, \"GET\")
2条回答
  •  别那么骄傲
    2021-01-22 03:00

    That's called tuple unpacking. In python, you can unpack tuples like this:

    a, b = (1, 2)
    

    See that on the right we have a tuple, packing values, and they are automatically "distributed" to the objects on the left.

    If a function returns a tuple, in can be unpacked as well:

    >>> def t():
    ...     return (1, 2)
    ... 
    >>> a, b = t()
    >>> a
    1
    >>> b
    2
    

    That's what's happening in your code.

提交回复
热议问题