Python built-in functions locals and globals (learning notes), pythonglobals

Source: Internet
Author: User

Python built-in functions locals and globals (learning notes), pythonglobals

Python built-in functions-locals and globals

These two functions provide dictionary-based access to local and global variables.
To understand these two functions, first understand the concept of namespace in python. Python uses something called a namespace to record the trajectory of a variable. A namespace is just a dictionary. Its key word is the variable name, and its value is the value of those variables. Actually, namespace can be accessed like a Python dictionary

Each function has its own namespace, called a local namespace. It records function variables, including function parameters and locally defined variables. Each module has its own namespace, which is called a global namespace. It records module variables, including functions, classes, other imported modules, module-level variables, and constants. There is also a built-in namespace, any model
Block can be accessed, it stores built-in functions and exceptions.

When a line of code uses the value of variable x, Python searches for variables in all available namespaces in the following order:

1. Local namespace-This refers to the method of the current function or class. If the function defines a local variable x, Python uses this variable and then stops searching.
2. Global namespace-This refers to the current module. If the module defines a variable, function, or class named x, Python uses this variable and then stops searching.
3. built-in namespace-Global for each module. As a final attempt, Python assumes that x is a built-in function or variable.

If Python cannot find x in these namespaces, it will discard the lookup and raise a NameError exception.
There is no variable named 'x.

Like many things in Python, namespace can be accessed directly at runtime. In particular, the local namespace can be accessed through the built-in locals function. The global (module level) namespace can be accessed through the globals function.

Locals Introduction

>>> Def test (arg): # function foo has two variables in its local namespace: arg (its value is passed into the function ), and z (defined in the function ). Z = 1 print locals () >>> test (4) # locals returns a dictionary of name/value pairs. The dictionary key is a variable name in the form of a string, and the dictionary value is the actual value of the variable. # When foo is called with 4, the dictionary containing two local variables of the function is printed: arg (4) and z (1 ). {'Z': 1, 'arg ': 4 }>>> test ('doulaixuexi') # locals can be used for all types of variables. {'Z': 1, 'arg ': 'doulaixuexi'} >>>

Globals Introduction

>>> from sys import *>>> print globals(){'setrecursionlimit': <built-in function setrecursionlimit>, 'dont_write_bytecode': False, 'getfilesystemencoding': <built-in function 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-in function exc_clear>, 'prefix': 'C:\\Python27', 'getrefcount': <built-in function getrefcount

We can see that the globals function returns a dictionary of global variables, including all imported variables.


# Example of the local variable function locals (locals returns a name/value pair dictionary .) :

Def foo (arg, a): x = 1y = 'xxxxxx' for I in range (10): j = 1 k = iprint locals () # Call the function to print the result foo () # {'A': 2, 'I': 9, 'K': 9, 'J': 1, 'arg ': 1, 'y': 'xxxxxx', 'x': 1}

 

The differences between from module import and import module. Use the import module to import the module itself,
However, it maintains its own namespace, Which is why you need to use the module name to access its functions or attributes (module. function)
. However, using from module import actually imports the specified functions and attributes into your own name from another module.
Space, which is why you can directly access them without referencing the modules from which they are sourced.

Locals is read-only and globals is not

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.