这是实际发生的情况:
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())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)