的Python:功能之间传递变量 [英] Python: Passing variables between functions

查看:99
本文介绍了的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. 初始化名单'作为一个空列表;调用主(这一点,至少,我知道我有权利... ...)

  2. 在defineAList(),分配一定的值代入名单;然后通过新的列表返回到主()

  3. 在main()中,调用useTheList(名单)

  4. 由于'名单'包含在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()?我倾向于更好地了解的东西,当我看到他们发生的事情,但是很多我见过的也用code正确的参数传递的例子,我不熟悉着呢,搞清楚的过程中发生了什么事对,我不是真的把这个概念的句柄。我使用的是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.

感谢您!

推荐答案

这是什么是真正发生的事情:

This is what is actually happening:

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天全站免登陆