您好,登錄后才能下訂單哦!
名稱空間正是存放名字x與1綁定關(guān)系的地方
1.2名稱空間種類
locals:是函數(shù)內(nèi)的名稱空間,包括局部變量和形參
globas:全局變量,函數(shù)定義所在模塊的名字空間
builtins:內(nèi)置模塊的名字空間
作用域即范圍:
全局范圍:全局存貨,全局有效
局部范圍:臨時(shí)存活,局部有效
查看作用域的方法globals(),locals()
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_import
lib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins'
(built-in)>, 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1
, 2, 3, 4, 5], 'b': ['a', 'b', 'c']}
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_import
lib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins'
(built-in)>, 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1
, 2, 3, 4, 5], 'b': ['a', 'b', 'c']}
>>>
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
level = 'L0'
n = 22
def func():
level = 'L1'
n = 33
print(locals())
def outer():
n = 44
level = 'L2'
print(locals(),n)
def inner():
level = 'L3'
print(locals(),n) #此外打印的n是多少?
inner()
outer()
func()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{'n': 33, 'level': 'L1'}
{'level': 'L2', 'n': 44} 44
{'level': 'L3', 'n': 44} 44
Process finished with exit code 0
LEGB代表變量查找順序:locals->enclosing function ->globals ->builtins
locals:函數(shù)內(nèi)的名字空間,包括局部變量和形參
enclosing外部嵌套函數(shù)的名字空間
globals全局變量,函數(shù)定義所在模塊的名字空間
builtins內(nèi)置模塊的名字空間
在外部函數(shù)中定義了一個(gè)內(nèi)部函數(shù),內(nèi)函數(shù)中使用了外部函數(shù)中的臨時(shí)變量,并且外部函數(shù)的返回值是內(nèi)部函數(shù)的引用,這樣就形成了閉包。
一般情況下我們認(rèn)為,如果一個(gè)函數(shù)結(jié)束,函數(shù)內(nèi)部所有的東西都會在內(nèi)存中被釋放掉,還給內(nèi)存,局部變量消失。但是閉包是一種特殊情況,如果外部函數(shù)結(jié)束的時(shí)候,發(fā)現(xiàn)自己的局部變量還會在內(nèi)部函數(shù)中使用,就會把這個(gè)局部變量綁定給內(nèi)部函數(shù),然后自己再結(jié)束。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
def outer(a):
b = 23
def inner():
print(a+b)
return inner
inner_fun = outer(12)
inner_fun()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
35
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
user_status = False
def login(func): #把要執(zhí)行的模塊從這里傳進(jìn)來
def inner():#再定義一層函數(shù)
_username = "vita" #假裝這是DB里存的用戶信息
_password = "123" #假裝這是DB里存的用戶信息
global user_status
if user_status == False:
username = input("user:")
password = input("pasword:")
if username == _username and password == _password:
print("welcome login....")
user_status = True
else:
print("wrong username or password!")
if user_status == True:
func() # 看這里看這里,只要驗(yàn)證通過了,就調(diào)用相應(yīng)功能
return inner #用戶調(diào)用login時(shí),只會返回inner的內(nèi)存地址,下次再調(diào)用時(shí)加上()才會執(zhí)行inner函數(shù)
@login
def america():
print("----歐美專區(qū)----")
america()
# 上面代碼相當(dāng)于下面的內(nèi)容
"函數(shù)開始執(zhí)行到@login時(shí)做的事"
# inner_fun = login(america)
"真正調(diào)用america()函數(shù),執(zhí)行inner函數(shù)"
# inner_fun()
def japan():
print("----日韓專區(qū)----")
@login
def henan():
print("----河南專區(qū)----")
henan()
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
user:vita
pasword:123
welcome login....
----歐美專區(qū)----
----河南專區(qū)----
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
user_status = False
def login(func): #把要執(zhí)行的模塊從這里傳進(jìn)來
def inner(*args,**kwargs):#再定義一層函數(shù)
_username = "vita" #假裝這是DB里存的用戶信息
_password = "123" #假裝這是DB里存的用戶信息
global user_status
if user_status == False:
username = input("user:")
password = input("pasword:")
if username == _username and password == _password:
print("welcome login....")
user_status = True
else:
print("wrong username or password!")
if user_status == True:
func(*args,**kwargs) # 看這里看這里,只要驗(yàn)證通過了,就調(diào)用相應(yīng)功能
return inner #用戶調(diào)用login時(shí),只會返回inner的內(nèi)存地址,下次再調(diào)用時(shí)加上()才會執(zhí)行inner函數(shù)
@login
def america(style):
print("style:",style)
print("----歐美專區(qū)----")
america("america")
# 上面的代碼相當(dāng)于
# inner_fun = login(america)
# inner_fun("america")
def japan():
print("----日韓專區(qū)----")
@login
def henan(style):
print("style:", style)
print("----河南專區(qū)----")
henan("china")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
user:vita
pasword:123
welcome login....
style: america
----歐美專區(qū)----
style: china
----河南專區(qū)----
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
user_status = False
def login(login_type):
def auth(func): #把要執(zhí)行的模塊從這里傳進(jìn)來
def inner(*args,**kwargs):#再定義一層函數(shù)
_username = "vita" #假裝這是DB里存的用戶信息
_password = "123" #假裝這是DB里存的用戶信息
global user_status
if user_status == False and login_type == "qq":
username = input("user:")
password = input("pasword:")
if username == _username and password == _password:
print("welcome login....")
user_status = True
else:
print("wrong username or password!")
if user_status == True and login_type == "qq":
func(*args,**kwargs) # 看這里看這里,只要驗(yàn)證通過了,就調(diào)用相應(yīng)功能
return inner #用戶調(diào)用login時(shí),只會返回inner的內(nèi)存地址,下次再調(diào)用時(shí)加上()才會執(zhí)行inner函數(shù)
return auth
@login("qq")
def america(style):
print("style:",style)
print("----歐美專區(qū)----")
america("america")
# 上面的代碼相當(dāng)于
# auth = login("qq")
# inner_fun = auth(america)
# inner_fun("america")
def japan():
print("----日韓專區(qū)----")
# 由于這里是不是qq登錄,所以沒有執(zhí)行
@login("weixin")
def henan(style):
print("style:", style)
print("----河南專區(qū)----")
henan("china")
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
user:vita
pasword:123
welcome login....
style: america
----歐美專區(qū)----
Process finished with exit code 0
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。