Python namespace and scope

Time:2021-12-28

Namespace

  • Namespaces: used to store the binding relationship between name and memory address. It is the division of stack area
  • Function: the namespace can store the same name in the stack area, so as to solve the naming conflict
  • There are three types of namespaces:
    1. Built in namespace
    2. Global namespace
    3. Local namespace

Built in namespace

  • Built in namespace: used to store the built-in names in the Python interpreter
  • Life cycle: generated when the Python interpreter is started, and destroyed when the Python interpreter is closed
  • For example:printinputint

Global namespace

  • Global namespace: the name generated by running the top-level code, or all the names except those defined in the function and built-in are global namespaces
  • Life cycle: generated when the python file is executed, and destroyed after the python file is executed
  • For example:
#Variable name a
a = 1

#Function name func
def func():
	pass

#Class name MyClass
class MyClass:
	pass

Local namespace

  • Local namespace: the name in the function generated during the operation of the function body code when calling the function
  • Life cycle: generated during function call and destroyed after function call
  • For example:
def func(x, y):
    pass

func(1, 2)

Namespace order

  • Namespace loading order: built in namespace – > global namespace – > local namespace
  • Namespace destruction order: local namespace – > global namespace – > built-in namespace
  • Search priority of Name: search one level up from the current location

Scope

global scope

  • Global scope: built in namespace, global namespace
  • characteristic:
    1. Global survival
    2. Globally valid: shared by all functions

Local scope

  • Local scope: local namespace
  • characteristic:
    1. Temporary survival
    2. Locally valid: valid within a function

Legb principle

The calling order of finding variables adopts the legb principle (i.e. the proximity principle)
B —— Builtin(Python); Namespace of Python built-in module (built-in scope)
G —— Global(module); Namespace (global scope) where the function is located outside
E —— Enclosing function locals; Scope of external nested function (nested scope)
L —— Local(function); Scope within the current function (local scope)
According to the principle of proximity, search from bottom to top and from inside to outside

Global and nonlocal

  • Global: global variables declared and modified
  • Nonlocal: modify local variables (local variables of the previous layer of the current function)
#Use of global
x = 1
def func():
	global x
	x = 2
func()
print(x)

#Use of nonlocal
def func1():
	x = 1
	def func2():
		nonlocal x
		x = 2
	func2()
	print(x)
func1()

summary

  1. Namespace is a division of stack. What really exists is stack area. A namespace is just a virtual partition
  2. Namespaces are prioritized and not nested
  3. The “nested relationship” of namespaces determines the search order of names
  4. The “nesting” of namespaces is based on the function definition stage

Recommended Today

Summary of import and export usage in JavaScript

import import 和 require 的区别 import 和js的发展历史息息相关,历史上 js没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。这对开发大型工程非常不方便。在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。也就是我们常见的 require 方法。 比如 `let { stat, exists, readFile } = require(‘fs’);` 。ES6 在语言标准的层面上,实现了模块功能。ES6 模块不是对象,而是通过export命令显式指定输出的代码,再通过import命令输入。 import 的几种用法: 1. import defaultName from ‘modules.js’; 2. import { export } from ‘modules’; 3. import { export as ex1 } from […]