Python locals globals

Source: Internet
Author: User

Python has two built-in functions,locals () and globals (), which provide a way for dictionary-based access to local and global variables.

First, there is a noun interpretation of the namespace. It's boring, but it's important, so be patient. Python uses something called a namespace to record the trajectory of a variable. A namespace is just a dictionary whose key word is the variable name, and the value of the dictionary is the value of those variables. In fact, namespaces can be accessed like a Python dictionary, and we'll see it in a minute.

There are several namespaces available in any one of the Python programs. Each function has its own namespace, called the local namespace, which records the variables of the function, including the parameters of the function and locally defined variables. Each module has its own namespace, called the global namespace, which records the variables of the module, including functions, classes, other imported modules, module-level variables, and constants. There is also a built-in namespace, which can be accessed by any module, which holds built-in functions and exceptions.

When a line of code is going to use the value of the variable x, Python will go to all available namespaces to find the variable, in the following order:

    1. Local namespace-refers to the method of the current function or class. If the function defines a local variable X,python will use this variable and then stop the search.
    2. Global namespace-Specifically, the current module. If the module defines a variable called X, a function or a class, Python will use that variable and then stop the search.
    3. Built-in namespaces-Global for each module. As a final attempt, Python will assume that X is a built-in function or variable.

If Python cannot find X in these namespaces, it discards the lookup and throws an Nameerror exception, passing there is no variable named ' x ' such a message

Like many things in Python, namespaces are directly accessible at run time. In particular, local namespaces can be accessed through the built-in locals function. Global (module level) namespaces can be accessed through the GLOBALS function

Loca ls  Introduction

1
2
3
4
5
6
7
8
9

Globals Introduction

1
2
3
4
5
6
7
8
9
10
11
12
>>> fromSysImport*
>>>PrintGlobals ()
{' Setrecursionlimit ': <built-inchfunction Setrecursionlimit>
' Dont_write_bytecode ': False,
' Getfilesystemencoding ': <built-inchfunction Getfilesystemencoding>
' Long_info ': Sys.long_info (bits_per_digit=15, sizeof_digit=2),
' stdout ': <idlelib.rpc.rpcproxy object at 0x02110850>
' Text ': <function text at 0x02111a70>
' Meta_path ': [],
' Exc_clear ': <built-inchfunction Exc_clear>
' prefix ': ' C:\\Python27 ', ' getrefcount ': <built-inchfunction Getrefcount
...... Omit a lot

You can find that the GLOBALS function returns a dictionary of global variables, including all imported variables.



from the difference between the module import and the import module. Using Import module, the module itself is imported,
But it maintains its own namespace, which is why you need to use the module name to access its function or genus (Module.function)
> the reason. However, using the From module import, you are actually importing the specified functions and properties from another module into your own name space, which is why you can access them directly without needing to reference the modules from which they originate.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.