Is there a way in Python to return a value via an output parameter?

后端 未结 5 1797
耶瑟儿~
耶瑟儿~ 2020-12-10 00:50

Some languages have the feature to return values using parameters also like C#. Let’s take a look at an example:

class OutClass
{
    static void OutMethod(o         


        
5条回答
  •  孤城傲影
    2020-12-10 00:57

    There is no reason to, since Python can return multiple values via a tuple:

    def func():
        return 1,2,3
    
    a,b,c = func()
    

    But you can also pass a mutable parameter, and return values via mutation of the object as well:

    def func(a):
        a.append(1)
        a.append(2)
        a.append(3)
    
    L=[]
    func(L)
    print(L)   # [1,2,3]
    

提交回复
热议问题