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'})。



猜你喜欢

转载自blog.csdn.net/qq_34765864/article/details/78012196