python 作用域

Posted 红尘陌上,独自行走

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 作用域相关的知识,希望对你有一定的参考价值。

什么是命名空间 == 对一个名字起作用的范围

# def test():
# print("----test----")

# import test
# test.test()

# from test import *


# LEGB规则 locals > enclosing function > globals > builtins(内件)# 查看内件 dir(__builtins__)
# L局部变量
# enclosing 闭包

a = 100 #全局变量

def test():
b = 200 #局部变量
print(locals)


# globals 查看全局变量
# locals 查看局部变量


# num = 100
# def test():
# num = 200
# print(num)

# test() #200


# 闭包
# num = 100
# def test1():
# num = 200
# def test2():
# # num = 300
# print(num)
# return test2

# ret = test1()
# ret()
























以上是关于python 作用域的主要内容,如果未能解决你的问题,请参考以下文章

python作用域和JavaScript作用域

#抬抬小手学Python# Python 之作用域下的 global 和 nonlocal 关键字

Python中的作用域

Python作用域

python中作用域

python函数作用域