Python知識(shí)分享網(wǎng) - 專業(yè)的Python學(xué)習(xí)網(wǎng)站 學(xué)Python,上Python222
Python 使用函數(shù)作為返回值
發(fā)布于:2023-09-12 10:53:05

Python 7天快速入門(mén)完整視頻教程https://www.bilibili.com/video/BV1o84y1Z7J1

 

Python   使用函數(shù)作為返回值

 

Python還支持使用函數(shù)作為其他函數(shù)的返回值

 

def test(bol):
    if bol:
        return add
    else:
        return sub


def add(x, y):
    return x + y


def sub(x, y):
    return x - y


b1 = test(True)
print(b1, b1(1, 2))
b2 = test(False)
print(b2, b2(1, 2))

 

運(yùn)行輸出:

 

<function add at 0x000002AFF9B4BC40> 3
<function sub at 0x000002AFF9CB0540> -1

 

 

 

轉(zhuǎn)載自: