2013-04-16 139 views
24

我已经花了几个小时在这里和其他地方阅读以及尝试,但我并不真正了解我确定的是一个非常基本的概念:传递值(作为变量)在不同的功能之间。Python:在函数之间传递变量

例如,我在一个函数一大堆值分配给一个列表,然后以后要使用在其他功能列表:根据我的理解是什么函数参数做,我

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() 

预计这样做如下:

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

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

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

所以,既然“回报”,显然没有做什么,我想这样做,或至少它不会做它,我觉得它应该的方式......这是什么实际上做?使用这个例子,你能告诉我,我需要做些什么才能从defineAList()中获取列表并在useTheList()中使用它?当我看到它们发生时,我倾向于更好地理解事物,但我见过的正确参数传递的很多示例也使用了我不熟悉的代码,并且在确定发生了什么的过程中,我我并没有真正理解这个概念。我使用2.7。

ETA-在过去,问了一个类似的问题,有人建议我使用全局变量而不是仅仅局部变量。如果在这里也是相关的 - 为了我所采用的课程的目的,我们不允许使用全局变量。

谢谢!

回答

26

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

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

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

谢谢您的详细解释!我知道我在这里错过了一些基本的东西。 :) – user2113818

+7

请不要命名的东西'列表',你失去了调用内置'列表'的能力:) –

+1

我不写我什么时候我真的打算跑的目的! – user2113818

7

你只是错过了一个关键步骤。你必须明确地将返回值传递给第二个函数。

def main(): 
    l = defineAList() 
    useTheList(l) 

或者:

def main(): 
    useTheList(defineAList()) 

或(虽然你不应该这样做也许乍一看不错,但全局只是因为你从长远来看悲痛!):

l = [] 

def defineAList(): 
    global l 
    l.extend(['1','2','3']) 

def main(): 
    global l 
    defineAList() 
    useTheList(l) 

该函数返回一个值,但不会像您的代码所假定的那样在任何类型的全局命名空间中创建符号。您必须实际捕获调用作用域中的返回值,然后将其用于后续操作。

+0

谢谢你 - 我感谢你提供的不仅是一个例子,但为什么它这样解释的功能。 – user2113818

3

你的回报是无用的,如果你不给它分配

list=defineAList() 
1

return返回一个值。无论你给这个价值什么名字都没有关系。返回它只是“传递出去”,以便别的东西可以使用它。如果你想使用它,你必须从外面抓住它:

lst = defineAList() 
useTheList(lst) 

从内defineAList返回list并不意味着“让它所以程序的所有的剩余可使用变量”。它意味着“传递这个变量,并给程序的其余部分一个机会来抓住它并使用它”。您需要将该值分配给函数外部的某个部分以便使用它。另外,正因为如此,没有必要提前定义您的清单list = []。在defineAList里面,你创建一个新列表并返回它;此列表与您在开始时使用list = []定义的列表没有任何关系。

顺便提一下,我将变量名从list更改为lst。使用list作为变量名称并不是一个好主意,因为它已经是内置Python类型的名称。如果您创建自己的变量list,则无法再访问内置的变量。

2

阅读名称空间的概念。在函数中分配变量时,只能在该函数的名称空间中分配它。 但显然你想在所有功能之间使用它。

def defineAList(): 
    #list = ['1','2','3'] this creates a new list, named list in the current namespace. 
    #same name, different list! 

    list.extend['1', '2', '3', '4'] #this uses a method of the existing list, which is in an outer namespace 
    print "For checking purposes: in defineAList, list is",list 
    return list 

或者,可以围绕它传递:

def main(): 
    new_list = defineAList() 
    useTheList(new_list) 
1

从一个函数传递变量作为参数传递给其它功能可以像这样

这样定义

函数来完成
def function1(): 
global a 
a=input("Enter any number\t") 

def function2(argument): 
print ("this is the entered number - ",argument) 

呼叫这样

function1() 
function2(a) 
+0

缩进是完全关闭的,应该先修复。其次,由于您指定了'global a',因此'function1()'中不需要'return'语句。通常使用'全局'变量显然是不好的味道,但在这样一个简单的应用程序中完全没有根据。参看[本](http://stackoverflow.com/questions/19158339/why-are-global-variables-evil)。 –

+0

@ N.Wouda我怎样才能执行这样的操作,而不使用全球? –

+1

'def function1():返回输入(“输入任意数字\ t”)'。然后,'a = function1()'和'function2(a)'。当然,你应该适当地对这段代码进行格式化,但是我不可能在评论中实现。 –