Built-in functions locals basic tutorial of Python () and globals () usage analysis

This paper describes examples of built-in functions locals basic tutorial of Python () and globals () usage. Share to you for your reference, as follows:

  1. The two major functions provided, the way a dictionary-based local variables and global variables.

python using a thing called the name space to record the track variables. A namespace is a dictionary whose key is a string variable name, its value is the actual value of the variable.

Namespace can be accessed like a Python dictionary, as.

Anywhere in a Python program, there are several namespaces available.

Each function has its own namespace, called the local namespace, it records the variable functions including parameters and local variables defined functions.

Each module has its own namespace, called the global namespace, it records the module's variables, including functions, classes, introducing the other modules, the module-level variables and constants.

There is a built-in namespace, accessible from any module, which holds built-in functions and exceptions.

  1. When a line of code to use the value of the variable x, Python will go to all the available space to find the variable name, in the order as follows:

Local namespace - specific to the current function or method of the class. If the function defines a local variable x, or a parameter x, it will use the Python, then stop searching.

Global namespace - specific to the current module. If the module defines a variable called x, function or class, Python will use it and stop searching.

Built-in namespace - global to all modules of. As a last attempt, Python will assume that x is a built-in function or variable.

If Python can not find space in these names x, it will give up and find a NameError trigger an exception, passing There is no variable named 'x' such a message.

  1. Namespace can be directly accessed at runtime. Local namespace is accessible via the built-in locals function. Global (module level) namespace can be accessed through the built-in globals function.

locals on the local (function) namespace, globals does for the global (module) namespace done.

Globals is more exciting, because a module's namespace include module-level variables and constants, it includes all the functions and classes defined in the module, as well as any thing to be imported into the module.

  1. Recall that the difference between from module import and import module?

Use import module, the module itself is imported, but it retains its own namespace, which is why you need to use the module name to access its functions or attributes: the reason module.function.

But using from module import, actually from another module specified functions and properties into your own namespace, which is why you can access them directly without referencing the reason they are derived modules.

With the globals function, you can actually see it happening.

  1. about locals () Example:
def foo(arg, a):
  x = 100
  y = 'hello python!'
  for i in range(10):
    j = 1
    k = i
  print locals()
foo(1,2)

result:

{'a': 2, 'i': 9, 'k': 9, 'j': 1, 'arg': 1, 'y': 'hello python!', 'x': 100}
  1. locals is read-only, can not be modified, but globals can be modified, because:

about locals () does not actually return the local namespace, it returns a copy. Therefore, modify it, modify a copy, while no effect on the actual value of the local variable name space.

globals () returns the actual global namespace, not a copy: the exact opposite behavior of locals.

So any changes will be on the dictionary globals returned directly affect the value of global variables.

#!/usr/bin/env python
#coding:utf-8
'''''This is my first python program!'''
z = 7 #定义全局变量
def foo(arg):
  x = 1
  print locals()
  print 'x=',x
  locals()['x'] = 2 #修改的是局部名字空间的拷贝,而实际的局部名字空间中的变量值并无影响。
  print locals()
  print "x=",x
foo(3)
print globals()
print 'z=',z
globals()["z"] = 8 #globals()返回的是实际的全局名字空间,修改变量z的值
print globals()
print "z=",z

result:

{'x': 1, 'arg': 3}
x= 1
{'x': 1, 'arg': 3}
x= 1
{'foo': <function foo at 0x02A17CF0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'E:\\workspace\\python day03\\main\\test.py', '__package__': None, '__name__': '__main__', 'z': 7, '__doc__': 'This is my first python program!'}
z= 7
{'foo': <function foo at 0x02A17CF0>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'E:\\workspace\\python day03\\main\\test.py', '__package__': None, '__name__': '__main__', 'z': 8, '__doc__': 'This is my first python program!'}
z= 8

Finally, we recommend a number of well-regarded public universities [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information on actual project data method, every day, programmers explain the timing Python technology, to share some of the learning and the need to pay attention to small details
Here Insert Picture Description

Published 20 original articles · won praise 0 · Views 3614

Guess you like

Origin blog.csdn.net/chengxun02/article/details/104996368