In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about Python global variables and local variables. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Before we learn about global and local variables, let's first learn about scope.
a = 10def func(): b = 20 def inner(): c = 30
For example, print a,b,c.
You can see that only a prints 10, b and c both report errors, which is the scope problem.
We create a new.py file as a module, in the module defined variables, such as a, is the module's global variables, acting globally, no matter where you can call,
But b and c are defined inside the function, we call them local variables, and can only act inside the function.
We're looking at a problem.
Can func and inner function call variable a?
Can func call variable c?
The answer is: a is a global variable and can be called anywhere.
But func function can not call c, then we regard func as a whole, then inner is the function defined inside func, c is the variable inside inner
Summary: Variable definitions can be called anywhere globally, defined within functions, and can only be used within functions.
Variable search order: LEGB
Local scope> Outer scope> Global in current module>Python built-in scope;
There are two methods for global and local variables: global and nonlocal.
Let's go straight to the example:
We can see that if we define a locally, then we call a directly and we get an error.
The call prints only after it is defined, and the a value of the global variable does not change
What if I now wanted to change the value of a in the local namespace? At this point, we need to use global.
We can see that the value of global variable a has been modified to 15.
Here's a nonlocal:
We see an error when printing a and b at this time. The error prompt is that b is not defined.
However, a does not report an error because a is a global variable and b is a local variable, so this error occurs. The correction is as follows:
Because b is a local variable, it is declared nonlocal before calling, and then it can be called.
namespace
namespace classification
Global namespace: Created when each module is loaded and executed, it records the variables defined in the module, including functions defined in the module, classes, other imported modules, module-level variables and constants.
Local namespace: The namespace owned by each function, which records all variables defined in the function, including function parameters and internally defined local variables.
Built-in namespaces: accessible to any module, with built-in functions and exceptions (e.g. input, print, str, list, tuple...).
Load order of namespaces
Built-in namespace (load before program runs)-> global namespace (program runs: load from top to bottom)-> local namespace (program runs: load when called)
The order in which namespaces are valued
Local calls: Local namespaces 1> Global namespaces 1> Built-in namespaces
Global calls: Global namespaces I> Built-in namespaces
To sum up, when looking for variables, look for small areas, layer by layer to large areas.
Thank you for reading! About "python global variable and local variable example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.