栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

退货有什么作用?一无所有

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

退货有什么作用?一无所有

return
从函数返回值:

def addseven(n):    return n + 7a = 9b = addseven(a)print(b)        # should be 16

它也可以用于退出功能:

def addseventosix(n):    if n != 6:        return    else:        return n + 7

但是,即使您

return
在函数中没有语句(或在不指定要返回值的情况下使用它),该函数仍会返回-
None

def functionthatisuseless(n):    n + 7print(functionthatisuseless(8))        # should output None

有时您可能想从一个函数返回多个值。但是,您不能有多个

return
语句-
控制流会在第一个语句之后留下该函数,因此它执行后的所有操作都不会执行。在Python中,我们通常使用元组和元组解包:

def addsevenandaddeight(n):    return (n+7, n+8)        # the parentheses aren't necessary, they are just for clarityseven, eight = addsevenandaddeight(0)print(seven)        # should be 7print(eight)        # should be 8

return
语句使您可以根据其他函数的结果调用函数:

def addseven(n):    return n+7def timeseight(n):    return n*8print(addseven(timeseight(9))# what the intepreter is doing (kind of):# print(addseven(72))    # 72 is what is returned when timeseight is called on 9# print(79)# 79


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/610828.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号