1. 程式人生 > >python 作用域

python 作用域

sin 全局 from -- num osi print ins def

什麽是命名空間 == 對一個名字起作用的範圍

# 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 作用域