In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the python scope, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Scope
The scope of a variable determines which part of the program can access which specific variable name. There are four scopes of Python, which are:
L (Local) local scope
In the function outside the E (Enclosing) closure function
G (Global) global scope
B (Built-in) built-in scope
Search according to the rule of L-> E-> G-> B, that is, if you can't find it locally, you will look for it outside the local area (such as closures). If you can't find it again, you will look for it globally, and then you will look for it in the built-in.
X = int (2.9) # built-in scope g_count = 0 # global scope def outer (): o_count = 1 # function outside the closure function def inner (): i_count = 2 # local scope
In Python, only modules (module), classes (class) and functions (def, lambda) will introduce new scopes, and other code blocks (such as if/elif/else/, try/except, for/while, etc.) will not introduce new scopes, that is, the variables defined in these statements can also be accessed externally.
Global and local variables
Variables defined inside the function have a local scope, and those defined outside the function have a global scope.
Local variables can only be accessed within the function in which they are declared, while global variables can be accessed throughout the program. When a function is called, all variable names declared within the function are added to the scope.
Global and nonlocal keywords
The global and nonlocal keywords are used when the internal scope wants to modify variables in the external scope.
Global
Num = 1def fun1 (): global num # needs to use the global keyword to declare print (num) num = 123 print (num) fun1 () the above instance output result: 1123
Nonlocal
The nonlocal keyword is required if you want to modify variables in a nested scope (enclosing scope, outer non-global scope).
Def outer (): num = 10 def inner (): nonlocal num # nonlocal keyword declaration num = 100print (num) inner () print (num) outer () the output results of the above examples: more than 100100 are all the contents of the article "how to use python scope". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.