Python two built-in functions locals and globals (learning notes) _python

Source: Internet
Author: User
Tags in python

Python two built-in functions--locals and Globals

These two functions primarily provide a way to access local and global variables based on the dictionary.
In understanding these two functions, first of all, to understand the name space concept in Python. Python uses something called a namespace to record the trajectory of a variable. The namespace is just a dictionary, its key word is the variable name, the dictionary value is the value of those variables. In fact, namespaces can be accessed like a Python dictionary.

Each function has its own namespace, called the local namespace, which records the variables of the function, including the parameters of the function and the 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 the built-in name space, any mode
Block can access it, storing built-in functions and exceptions.

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

1. Local namespace-a method that refers specifically to 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 namespaces-specifically the current module. If the module defines a variable called X, a function or class, Python will use this variable and then stop the search.
3. Built-in namespaces-is global for each module. As a last attempt, Python assumes that x is a built-in function or variable.

If Python cannot find X in these namespaces, it discards the lookup and throws a Nameerror exception, passing
There is no variable named a message like ' X '.

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

Locals introduction

>>> def Test (ARG):
#函数 foo has two variables in its local namespace: ARG (its value is passed into the function), and z (which is defined in the function).
  z = 1
  print locals ()
>>> Test (4)
#locals Returns a dictionary of name/value pairs. The key word in this dictionary is a variable name in the form of a string, and the value of the dictionary is the actual value of the variable.
#所以用 4来 call Foo, a 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

This shows that the GLOBALS function returns a dictionary of global variables, including all imported variables.


#局部变量函数locals例子 (Locals returns a dictionary of name/value pairs. ):

def foo (ARG, a):
	x = 1
	y = ' xxxxxx ' for
	I in Range (Ten):
		j = 1
		k = i
	print locals ()


#调用函 Number of print results	
foo (1,2)
#{' a ': 2, ' I ': 9, ' K ': 9, ' J ': 1, ' arg ': 1, ' y ': ' xxxxxx ', ' X ': 1}

The difference between the From module import and the import module. The module itself is imported using the Import module,
But it keeps its own namespace, which is why you need to use the module name to access its functions or properties (module.function)
The reason. But using the From module import actually imports the specified functions and properties from another module into your own name
Space, which is why you can access them directly without having to refer to the module they are from.

Locals is read-only, globals not

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.