简体   繁体   English

另一个UnboundLocalError:在赋值问题之前引用的局部变量

[英]Another UnboundLocalError: local variable referenced before assignment Issue

I stumbled upon a situation that shutters my understanding of Pythons variable scope. 我偶然发现了一个让我理解Pythons变量范围的情况。

Here is the code: 这是代码:

transaction_id = None


def parseFileContent(hostID,marketID, content, writeToDB=False):
    features = _buildObjects(StringIO.StringIO(content))

    for feature in features:
        featureID =  adapter.addFeature(feature.name,boris)
        print transaction_id #breaks here UnboundLocalError: local variable 'transaction_id' referenced before assignment

        transaction_id = adapter.addFeatureTransactionToQueue(featureID, result[0], result[1], Command.ADD, boris, trans_id = transaction_id)

If I replace last line with 如果我替换最后一行

       adapter.addFeatureTransactionToQueue(featureID, result[0], result[1], Command.ADD, boris, trans_id = transaction_id)

Everything works. 一切正常。 I need to understand what python dislikes about me printing the value in the first scenario. 我需要了解python不喜欢我在第一个场景中打印值。

The Python compiler marks a name as local to a function if you assign to it . 如果分配给函数,Python编译器会将名称标记为函数的本地名称。 Your last line assigns to transaction_id so it is seen as a local name, not a global. 您的最后一行分配给transaction_id因此它被视为本地名称,而不是全局名称。

You need to tell the compiler explicitly that transaction_id is a global, by using the global keyword inside the function: 您需要通过使用函数内部的global关键字明确告诉编译器transaction_id是全局的:

def parseFileContent(hostID,marketID, content, writeToDB=False):
    global transaction_id

If there is no assignment, a name is considered non-local instead and you do not need to mark it. 如果没有分配,则名称将被视为非本地名称,您无需对其进行标记。

Since Python doesn't have variable declarations, it needs another way to tell what's local to what scope. 由于Python没有变量声明,因此需要另一种方法来告诉什么是本地范围。 It does so by defining that anything a function assigns to is local to that function unless overridden by a global declaration. 它通过定义函数分配的任何内容对该函数是本地的,除非被global声明覆盖。 Thus, assigning to transaction_id makes it a local, and the print tries to print the unassigned local and fails. 因此,分配给transaction_id使其成为本地,并且print尝试打印未分配的本地并失败。

When you assign to a name inside a function, it's a local name. 分配给函数内的名称时,它是本地名称。 Python will only ever treat the name as local inside that function, even if you have a global variable with the same name. 即使您有一个具有相同名称的全局变量,Python也只会将该名称视为该函数内的本地名称。 This is different from some other languages, where if a local is not found, a global with the same name will be used if it exists. 这与其他一些语言不同,如果找不到本地语,则如果存在,则将使用具有相同名称的全局语言。

You should avoid global variables. 你应该避免全局变量。 There is no reason for your transaction_id to be global in this case. 在这种情况下,您的transaction_id没有理由是全局的。 So just put the None assignment inside the function. 所以只需将None赋值放在函数中。 If you want access to the last transaction ID after the function executes, simply return it from the function. 如果要在函数执行后访问最后一个事务ID,只需从函数返回它。

def parseFileContent(hostID,marketID, content, writeToDB=False):
    features = _buildObjects(StringIO.StringIO(content))
    transaction_id = None     # <----------------------

    for feature in features:
        featureID =  adapter.addFeature(feature.name,boris)
        print transaction_id  # now works

        transaction_id = adapter.addFeatureTransactionToQueue(featureID, 
            result[0], result[1], Command.ADD, boris, trans_id=transaction_id)

     return transaction_id    # <----------------------

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnboundLocalError:分配问题之前引用的局部变量 - UnboundLocalError: local variable referenced before assignment issue Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment Python基本问题(UnboundLocalError:分配前已引用局部变量“ normal”) - Basic Python Issue (UnboundLocalError: local variable 'normal' referenced before assignment) python中的怪异问题:UnboundLocalError:分配前引用了局部变量“ y” - weird issue in python: UnboundLocalError: local variable 'y' referenced before assignment UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM