Python:在函数之间传递变量

Python:在函数之间传递变量,第1张

Python:在函数之间传递变量

这是实际发生的情况:

global_list = []def defineAList():    local_list = ['1','2','3']    print "For checking purposes: in defineAList, list is", local_list     return local_listdef useTheList(passed_list):    print "For checking purposes: in useTheList, list is", passed_listdef main():    # returned list is ignored    returned_list = defineAList()    # passed_list inside useTheList is set to global_list    useTheList(global_list)main()

这就是你想要的:

def defineAList():    local_list = ['1','2','3']    print "For checking purposes: in defineAList, list is", local_list     return local_listdef useTheList(passed_list):    print "For checking purposes: in useTheList, list is", passed_listdef main():    # returned list is ignored    returned_list = defineAList()    # passed_list inside useTheList is set to what is returned from defineAList    useTheList(returned_list)main()

您甚至可以跳过临时

returned_list
变量,并将返回值直接传递给
useTheList

def main():    # passed_list inside useTheList is set to what is returned from defineAList    useTheList(defineAList())


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5643608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存