In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-05-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the python namespaces and scope of the example analysis, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
The function inside the function can only be called inside the function, not outside the function. You will know why this happens through the following study.
I. namespace
Namespaces (name spaces): in the chapter on memory management, we mentioned that the creation of variables is actually opening up a new space in memory. But we have been avoiding the storage of variable names, in fact, there is a memory space in memory to store the binding relationship between variable names and variables, and this space is called namespace, and the real place to store the name is the stack area. the namespace only divides the stack into several parts, and with the namespace, we can store the same name in the stack area.
1.1 built-in namespace
Built-in namespaces: store the names included in the Pyhton interpreter, such as int, float, len
Life cycle: takes effect when the interpreter starts and expires when the interpreter is closed
1.2 Global namespace
Global namespaces: except for built-in and local names, the rest are stored in global namespaces, such as x, func, l, z in the following code
Life cycle: takes effect when the file is executed and expires after the end of the file execution
1.3 Local namespaces
Local namespace: used to store the name generated by the function body during the function call, such as f2 in the following code
Life cycle: takes effect during the function call when the file is executed and expires after the end of the function execution
Note: even for the same function, even if it is empty, if we call it multiple times, the call produces a local namespace at a time
1.4 loading order
Because the .py file is opened by the Python interpreter, the file must not be opened until the loading of the built-in namespace in the Python interpreter finishes. Global namespaces will only be generated at this time, but local namespaces will only be generated when a function in the file is called, so the loading order of namespaces is: built-in-"global -" local.
1.5 search order
Because namespaces are used to store the binding relationship between variable names and values, whenever you want to look for names, you must find them in one of three, in the following order:
Start with the current location, and if the current location is a local namespace, the search order is: local-"global -" built-in.
II. Scope
Domain refers to the area, and the scope is the area of action.
2.1 Global scope
Global scope: globally valid, shared by all functions, including built-in and global namespaces.
2.2 Local scope
Local scope: local small, temporary storage, including only local namespaces.
2.3 pay attention
It should be noted that the scope relationship is fixed in the function definition phase and has nothing to do with the function call.
2.4 function object + scope Application
Third, supplementary knowledge point 3.1 global keyword
Modify variables in the global scope.
3.2 nonlocal keyword
Modify variables in the local scope.
3.3 pay attention
If you want to modify the global mutable type locally, you don't need any declaration, you can modify it directly.
Locally, if you want to modify the global immutable type, you need to use the global declaration to declare a global variable, which can be modified directly.
The above is all the content of the article "sample Analysis of namespaces and scopes in python". 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.