将参数传递给内部函数

时间:2018-04-16 09:46:57

标签: python parameter-passing

我有一个内部功能的功能。我想知道将变量传递给内部函数的正确方法是什么。从我可以看到默认情况下传递的表,虽然我不确定这是一个未记录的变通方法或python设计。

def function():
    def inner_function():
        if a[0] > b[0]:
            print("a[0] = {0}, b[0] = {1}".format(a[0], b[0]))
            tmp = c
            c = d
            d = tmp

    a = [4, 3]
    b = [2, 1]
    c = 1
    d = 2

    inner_function()

function()

python test.py输出:

  

$ python test.py a [0] = 4,b [0] = 2回溯(最近一次呼叫最后一次):

     

文件“test.py”,第16行,

     

function()

   File "test.py", line 14, in function
     

inner_function()

   File "test.py", line 5, in inner_function
     

tmp = c

     

UnboundLocalError:在赋值之前引用的局部变量'c'

将变量从“function”传递到“inner_function”的正确方法是什么?除了参数之外还有其他方法吗?为什么“c”变量引用出错而不是“a”表?

4 个答案:

答案 0 :(得分:1)

AFAIK c因为在inner_function内分配而导致错误,因此它与c中定义的function变量不同。变量ab有效,因为它们仅在inner_function处读取,因此它们未被重新定义。将cd重命名为new_cnew_d会使其有效。

https://pyfiddle.io/fiddle/184e1778-adb7-4759-8951-da699751c31e/

有关Python nested functions variable scoping

的更多信息
def function():
    def inner_function():
        if a[0] > b[0]:
            print("a[0] = {0}, b[0] = {1}".format(a[0], b[0]))
            tmp = c
            new_c = d
            new_d = tmp

    a = [4, 3]
    b = [2, 1]
    c = 1
    d = 2

    inner_function()

function()

答案 1 :(得分:0)

你需要将c和d变量说成全局(顺便说一下这不是一个好主意)

def function():
global c,d
a = [4, 3]
b = [2, 1]
c = 1
d = 2
def inner_function():
    if a[0] > b[0]:
        global c,d
        print("a[0] = {0}, b[0] = {1}".format(a[0], b[0]))
        tmp = c
        c = d
        d = tmp
inner_function()

答案 2 :(得分:0)

我猜Pythonic的方式肯定是指鸭子和兔子,甚至可能是骑士。我也将@Metareven作为参数传递给他们,因为Python有一种非常简洁的方式来处理它们。这样您就不必担心@global变量了。而且你对所发生的事情以及所提出的事情有了一个很好的了解。

def function(duck):
    def inner_function(rabbit):
        if rabbit[0][0] > rabbit[1][0]:
            print("a[0] aka the first one behind the rabbit = {0}, \nb[0] aka the second one behind the rabbit = {1}".format(rabbit[0], rabbit[1]))
            tmp = rabbit[2]
            rabbit[2] = rabbit[3]
            rabbit[3] = tmp
    inner_function(duck)

#Let's sort out the arguments
a = [4, 3]
b = [2, 1]
c = 1
d = 2


function([a,b,c,d])

函数调用返回以下内容:

python innner.py
a[0] aka the first one behind the rabbit = [4, 3], 
b[0] aka the second one behind the rabbit = [2, 1]

这是否回答了你的问题?

答案 3 :(得分:0)

虽然您的问题已得到解答,但我不确定您的代码为何会产生此错误。

无论如何要明确导致问题的一行是c = d,尽管你的翻译不同意。 让我解释一下。您位于inner_function()内,没有这些行(c = dd = tmp),您指的是变量abc和{{ 1}}在d之外分配。因此,您隐含地将这些变量(inner_function()abc)称为d
在为内部函数内部的变量赋值时,解释器会将其视为此函数的局部变量。在这种情况下,global现在被认为是本地的(因为即使在解释器抱怨c的语句之后,inner_function()内也有一个赋值。所以,通常情况下,解释器会抱怨一个变量,它没有赋值,但无论如何都会被访问。

所以为了说清楚, python不区分变量类型(例如java)。

  

将变量从“function”传递到的正确方法是什么?   “inner_function”?

使用参数是正确的方法。

  

除参数外还有其他方法吗?

正如其他人所提到的,你可以使用全局变量,但不推荐它作为方法。

  

为什么“c”变量引用出错而不是“a”表?

正如我之前提到的,不同类型的变量之间没有区别。使它们与众不同的是,您在变量tmp = c中分配了一个值,但只是访问了c的值,例如在另一种情况下。

这个specific answer(@ raul.vila也提到过)提供了一个很好的解释。

最后,因为目前还不清楚你想要实现的目标。如果您尝试在内部函数中打印全局(甚至隐式)变量,或者您尝试在内部函数内部更改全局变量的值,则会有所不同。

相关问题