In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to solve UnboundLocalError errors encountered by Python developers", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve UnboundLocalError errors encountered by Python developers" article.
For example, the following code increments x by 1 in the foo function:
X = 10
Def foo ():
X + = 1
Print (x)
Foo ()
When foo () is called, the stack log tells us that the local variable x is referenced elsewhere before it is assigned, in other words, x is used before it is defined in the current scope.
Traceback (most recent call last):
File "unboundtest.py", line 8, in
Foo ()
File "unboundtest.py", line 4, in foo
X + = 1
UnboundLocalError: local variable'x 'referenced before assignment
Why tell us that x is referenced without a value when it is clearly defined outside the function foo?
Because this is a mistake that almost everyone encounters, Python officially included the problem in its FAQ, which says:
This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope.
When you assign a value to a variable in a local scope, the variable becomes a local variable, whether it is initialized externally or not. If there are variables with the same name in the external scope, then these global variables with the same name will be hidden as invisible to the local space.
In this case, x + = 1 is equivalent to x = x + 1, which is first performed by x + 1, and then assigned to x. According to the official explanation, Python thinks that x is a local variable for the function foo, because it has an assignment operation. Since x is a local variable, x is referenced when performing the addition operation, so a UnboundLocalError exception is thrown.
So, how to solve the exception? The solution is very simple. Python provides a keyword globlal, which is used to explicitly identify x as a global variable. When x is set to a global variable, the add operation will go to the global namespace to look for x.
If the variable x is not reassigned in the local scope, but is referenced directly, Python looks for the variables in the order of the rules of LEGB (local- >-enclosing- > global- > built-in).
Lst = [1,2,3]
Def foo ():
Lst.append (5) # executes normally. Lst is not reassigned. First, search in the local scope, then look for lst in the global scope if you can't find it.
# lst + = [5] # UnboundLocalError error because the value was reassigned and referenced in the local scope without being defined
Foo ()
Print (lst)
After this problem is solved, let's take a look at another similar problem:
Def external ():
X = 10
Def internal ():
X + = 1
Print (x)
Internal ()
External ()
This is a Python closure, when you execute the external function, you know that the same UnboundLocalError error will be reported when you look at the code, so is it feasible to use global to fix it? Try it:
Def external ():
X = 10
Def internal ():
Global x
X + = 1
Print (x)
Internal ()
External ()
There is a new error, NameError: global name'x'is not defined, all variables x is not defined, think about it, you see x is a local variable defined in the external function, now you want to declare the x in the internal function as a global variable, Python can not find x in the global scope space, so there is NameError, so how to solve this problem? If you are using Python3, congratulations, a new keyword nolocal has been added to Python3 to represent non-local variables.
With dis.dis (foo), you can see the execution process of the Python internal bytecode instruction. From the bytecode, you can see that the instruction of the third line of code x = xroom1 is LOAD_FAST, and LOAD_FAST 0 (x) means that the Python interpreter loads x from the local scope, and x can not be found, so UnboundLocalError appears.
3 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
7 STORE_FAST 0 (x)
4 10 LOAD_FAST 0 (x)
13 PRINT_ITEM
14 PRINT_NEWLINE
15 LOAD_CONST 0 (None)
The above 18 RETURN_VALUE is about the article "how to solve UnboundLocalError errors encountered by Python developers". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.