1. 程式人生 > >python 函式返回值問題

python 函式返回值問題

#!/usr/bin/env python

def test1():
    print ("in the test1")

def test2():
    print ("in the test2")
    return 0
def test3():
    print ("in the test3")
    return 1, 'hello', ['alex', 'wupriqu'], {'name': "pi"}

x = test1()
y = test2()
z = test3()

print x
print y
print z

1: 如果像test1一樣函式沒有return, 那麼函式預設返回的值為 None.

2: 如果像test2一樣函式有return, 那麼函式返回的值為return的值.

3: 如果像test3一樣函式有return,但是函式返回的是多個值, 那麼函式返回的值為return的值.就是多個值,但是實質上是一個值和c語言是一樣的,只不過python把這些多個值返回為一個tuple(元祖, 實際就是返回一個值。test3的返回值結果: (1, 'hello', ['alex', 'wupriqu'], {'name': 'pi'})。