Passing functions which have multiple return values as arguments in Python

前端 未结 3 491
忘掉有多难
忘掉有多难 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:44

    printa(*cord())
    

    The * here is an argument expansion operator... well I forget what it's technically called, but in this context it takes a list or tuple and expands it out so the function sees each list/tuple element as a separate argument.

    It's basically the reverse of the * you might use to capture all non-keyword arguments in a function definition:

    def fn(*args):
        # args is now a tuple of the non-keyworded arguments
        print args
    
    fn(1, 2, 3, 4, 5)
    

    prints (1, 2, 3, 4, 5)

    fn(*[1, 2, 3, 4, 5])
    

    does the same.

提交回复
热议问题