Meaning of using commas and underscores with Python assignment operator?

后端 未结 4 1937
我在风中等你
我在风中等你 2020-11-27 15:32

Reading through Peter Norvig\'s Solving Every Sudoku Puzzle essay, I\'ve encountered a few Python idioms that I\'ve never seen before.

I\'m aware that a function can

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 16:22

    The _ in the Python shell also refers to the value of the last operation. Hence

    >>> 1
    1
    >>> _
    1
    

    The commas refer to tuple unpacking. What happens is that the return value is a tuple, and so it is unpacked into the variables separated by commas, in the order of the tuple's elements.

提交回复
热议问题