【问题标题】:Passing parameter to inner function将参数传递给内部函数
【发布时间】:2018-09-26 00:19:55
【问题描述】:

我有一个具有内部功能的功能。我想知道将变量传递给内部函数的正确方法是什么。从我所见,默认情况下会传递表格,尽管我不确定这是未记录的解决方法还是 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 Traceback(最近一次调用最后一次):

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

函数()

   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”表有错误?

【问题讨论】:

标签: python parameter-passing


【解决方案1】:

AFAIK c 抛出错误,因为在 inner_function 内分配,因此它与 function 中定义的 c 变量不同。变量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()

【讨论】:

    【解决方案2】:

    您需要将 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()
    

    【讨论】:

      【解决方案3】:

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

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

      为了清楚起见,python 不区分变量的类型(例如 java 所做的)。

      将变量从“函数”传递到 “inner_function”?

      使用参数是正确的方法。

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

      正如其他人所说,您可以使用全局变量,但不建议这样做。

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

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

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

      最后,因为尚不清楚您要在这里实现什么。如果您尝试在内部函数中打印全局(甚至是隐式)变量,或者您尝试在内部函数中更改全局变量的值,则会有所不同。

      【讨论】:

        【解决方案4】:

        我猜 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]
        

        这是否回答了您的问题?

        【讨论】:

          猜你喜欢
          • 2015-02-17
          • 2021-09-21
          • 2015-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-10
          • 2015-10-05
          相关资源
          最近更新 更多