Python:在函数之间传递变量 [英] Python: Passing variables between functions

查看:36
本文介绍了Python:在函数之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时我一直在这里和其他地方阅读并进行实验,但我并没有真正理解我确定的一个非常基本的概念:在不同的函数之间传递值(作为变量).

I've spent the past few hours reading around in here and elsewhere, as well as experimenting, but I'm not really understanding what I am sure is a very basic concept: passing values (as variables) between different functions.

例如,我在一个函数中将一大堆值分配给一个列表,然后想稍后在另一个函数中使用该列表:

For example, I assign a whole bunch of values to a list in one function, then want to use that list in another function later:

list = []

def defineAList():
    list = ['1','2','3']
    print "For checking purposes: in defineAList, list is",list
    return list

def useTheList(list):
    print "For checking purposes: in useTheList, list is",list

def main():
    defineAList()
    useTheList(list)

main()

根据我对函数参数作用的理解,我希望这样做如下:

Based on my understanding of what function arguments do, I would expect this to do as follows:

  1. 将'list'初始化为一个空列表;调用 main(至少,我知道我是对的......)
  2. 在defineAList()中,将某些值分配到列表中;然后将新列表传回 main()
  3. 在 main() 中,调用 useTheList(list)
  4. 由于list"包含在 useTheList 函数的参数中,我希望 useTheList 现在将使用由 defineAList() 定义的列表,而不是在调用 main 之前定义的空列表.

然而,这显然是一种错误的理解.我的输出是:

However, this is obviously a faulty understanding. My output is:

For checking purposes: in defineAList, list is ['1', '2', '3']
For checking purposes: in useTheList, list is []

所以,既然返回"显然没有按照我的想法去做,或者至少它没有按照我认为的方式去做……它实际上做了什么?你能不能用这个例子告诉我,我需要做什么才能从defineAList()中获取列表并在useTheList()中使用它?当我看到事情发生时,我倾向于更好地理解它们,但是我见过的许多正确传递参数的示例也使用了我还不熟悉的代码,并且在弄清楚发生了什么的过程中,我我并没有真正掌握这个概念.我使用的是 2.7.

So, since "return" obviously does not do what I think it does, or at least it does not do it the way I think it should... what does it actually do? Could you please show me, using this example, what I would have to do to take the list from defineAList() and use it within useTheList()? I tend to understand things better when I see them happening, but a lot of the examples of proper argument-passing I've seen also use code I'm not familiar with yet, and in the process of figuring out what's going on, I'm not really getting a handle on this concept. I'm using 2.7.

ETA- 过去,问一个类似的问题,有人建议我使用全局变量而不是本地变量.如果它也与此处相关 - 就我正在上课的目的而言,我们不允许使用全局变量.

ETA- in the past, asking a similar question, it was suggested that I use a global variable instead of just locals. If it will be relevant here also- for the purposes of the class I'm taking, we're not permitted to use globals.

谢谢!

推荐答案

这是实际发生的事情:

global_list = []

def defineAList():
    local_list = ['1','2','3']
    print "For checking purposes: in defineAList, list is", local_list 
    return local_list 

def useTheList(passed_list):
    print "For checking purposes: in useTheList, list is", passed_list

def 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_list 

def useTheList(passed_list):
    print "For checking purposes: in useTheList, list is", passed_list

def 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:

You can even skip the temporary returned_list and pass the returned value directly to useTheList:

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

这篇关于Python:在函数之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆