內(nèi)置命令空間全局命名空間局部命名空間區(qū)別func函數(shù)內(nèi)存地址NotImplementedError內(nèi)置命令空間就是python解釋器 ,啟動就可以使用的名字存儲在內(nèi)置命名空間中 內(nèi)置的名字在啟動解釋器的時候被加載進(jìn)內(nèi)" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > python命名空間

python命名空間

時間:2023-05-08 04:45:02 | 來源:網(wǎng)站運(yùn)營

時間:2023-05-08 04:45:02 來源:網(wǎng)站運(yùn)營

python命名空間:

內(nèi)置命令空間

就是python解釋器 ,啟動就可以使用的名字存儲在內(nèi)置命名空間中 內(nèi)置的名字在啟動解釋器的時候被加載進(jìn)內(nèi)存里

全局命名空間

自己寫的代碼,但不是函數(shù)中的代碼

是在程序從上到下被執(zhí)行的過程中依次加載進(jìn)內(nèi)存的 放置了我們設(shè)置的所有變量名和函數(shù)名

局部命名空間

就是函數(shù)內(nèi)部定義的名字 當(dāng)調(diào)用函數(shù)的時候,才會產(chǎn)生這個名稱空間,隨著函數(shù)執(zhí)行的結(jié)束,這個命名空間就消失了

區(qū)別

1、在局部: 可以使用全局,內(nèi)置命名空間中的名字

2、在全局:可以使用內(nèi)置命名空間中的名字,但是不能使用局部中變量使用

例子:

def func(): a = 1func()print(a)運(yùn)行結(jié)果:

NameError: name 'a' is not defined3、在內(nèi)置: 不能使用局部和全局的名字的

順序是這樣的: 內(nèi)置>全局>局部

例子:

def max(): print("in max func")max()運(yùn)行結(jié)果:

in max func在正常情況下,直接使用內(nèi)置的名字 當(dāng)我們在全局定義了和內(nèi)置名字空間中同名的名字時,就會使用全局的名字 一級一級找

func函數(shù)內(nèi)存地址

# 函數(shù)名() 函數(shù)的調(diào)用# 加入id 就是函數(shù)的內(nèi)存地址def max(): print("in max func")print(max)print(id(max))運(yùn)行結(jié)果:

<function max at 0x0000025D7091D9D8>2600343820760

NotImplementedError

Python編程中raise可以實(shí)現(xiàn)報(bào)出錯誤的功能,而報(bào)錯的條件可以由程序員自己去定制。

在面向?qū)ο缶幊讨校梢韵阮A(yù)留一個方法接口不實(shí)現(xiàn),在其子類中實(shí)現(xiàn)。

如果要求其子類一定要實(shí)現(xiàn),不實(shí)現(xiàn)的時候會導(dǎo)致問題,那么采用raise的方式就很好。

而此時產(chǎn)生的問題分類是NotImplementedError

例子:

class ClassDemo(object): def run(self): raise NotImplementedErrorclass ChildClass(ClassDemo): def run(self): print("Hello world")ChildClass().run()


例子:

class ClassDemo(object): def run(self): raise NotImplementedError def wrong(self): # Will raise a TypeError NotImplemented = "don't do this" return NotImplementedclass ChildClass(ClassDemo): def run(self): print("Hello world") def wrong(self): print("wrong")ChildClass().run() # Hello worldwrong = ClassDemo().wrong()print(wrong) # don't do this這里區(qū)分下 NotImplemented && NotImplementedError

type(NotImplemented)<class 'NotImplementedType'> type(NotImplementedError)<class 'type'>issubclass(NotImplementedError,Exception)TrueNotImplemented 是 Python 內(nèi)建命名空間內(nèi)僅有的 6 個常量(Python 中沒有真正的常量)之一, 其它幾個分別是 False、True、None、Ellipsis 和__debug__

和 Ellipsis 一樣,NotImplemented 也可以被重新賦值: NotImplemented = "don't do this"

兩者是什么關(guān)系呢?答案是“沒啥關(guān)系”

Python 中 NotImplemented 廣泛應(yīng)用于二元魔術(shù)方法中,比如 __eq__()、__lt__()等等,表示該類型無法和其它類型進(jìn)行對應(yīng)的二元運(yùn)算

例子:

class A(object): def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, A): print('Comparing an A with an A') return other.value == self.value if isinstance(other, B): print('Comparing an A with a B') return other.value == self.value print('Could not compare A with the other class') return NotImplementedclass B(object): def __init__(self, value): self.value = value def __eq__(self, other): # raise NotImplementedError if isinstance(other, B): print('Comparing a B with another B') return other.value == self.value print('Could not compare B with the other class') return NotImplementeda, b = A(1), B(1)aa, bb = A(1), B(1)a == aa # Trueb == bb # Truea == b # Trueb == a # True運(yùn)行結(jié)果:

Comparing an A with an AComparing a B with another BComparing an A with a B說明 == 運(yùn)算符執(zhí)行時會先尋找 B 的 __eq__()方法, 遇到 NotImplemented 返回值則反過來去尋找 A 的 __eq__() 方法。

什么時候該使用 NotImplementedError?

NotImplementedError 是 RuntimeError 的子類: issubclass(NotImplementedError, RuntimeError) # True
官網(wǎng) 的建議是當(dāng)你需要一個方法必須覆蓋才能使用時,其效果類似于 Java 中的接口,用于定義一個未實(shí)現(xiàn)的抽象方法。

關(guān)鍵詞:空間,命名

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉