Passing functions which have multiple return values as arguments in Python

前端 未结 3 470
忘掉有多难
忘掉有多难 2021-01-01 19:57

So, Python functions can return multiple values. It struck me that it would be convenient (though a bit less readable) if the following were possible.

a = [[         


        
3条回答
  •  清酒与你
    2021-01-01 20:37

    Actually, Python doesn't really return multiple values, it returns one value which can be multiple values packed into a tuple. Which means that you need to "unpack" the returned value in order to have multiples. A statement like

    x,y = cord()
    

    does that, but directly using the return value as you did in

    printa(cord())
    

    doesn't, that's why you need to use the asterisk. Perhaps a nice term for it might be "implicit tuple unpacking" or "tuple unpacking without assignment".

提交回复
热议问题