在Python解釋器中,有少許的常量存在於內置命名空間中html
所謂常量就是指不變化的量,雖然在python中的常量有能夠被從新賦值的常量,可是強烈建議不要對其從新賦值,不然會影響python解釋器正確執行python程序python
False:shell
True數據結構
Noneapp
NoneType
類型的惟一值.None
常常用於表示缺乏值,當由於默認參數未傳遞給函數時.給None
賦值也是非法的,後引起SyntaxErrorNotImplemented函數
__eq__()
,__lt__()
,__add__()
,__rsub__()
等)表示操做沒有針對其餘類型實現,爲了相同的目的能夠經過就地二進制特殊方法(例如__imul__()
,__rightnd__()
等)返回,它的邏輯爲真.註解:當二進制(或就地)方法返回
NotImplemented
時,解釋器將常識對另外一種類型(或其餘一些回滾操做,取決於運算符)的反射操做。 若是全部嘗試都返回NotImplemented
,則解釋器將引起適當的異常。 錯誤返回的NotImplemented
將致使誤導性錯誤消息或返回到Python代碼中的NotImplemented
值。ui
NotImplemented是說明當前代碼沒有實現對調用方法的支持,不錯產生錯誤.this
它的布爾值爲真:spa
>>> bool(NotImplemented)
True
複製代碼
下面用代碼展現這個常量的做用,定義兩個類,實現了__eq__()
方法:命令行
class A:
def __init__(self, value):
self.value = value
def __eq__(self, other):
""" 傳進的other是A的實例或者B的實例,都調用此方法進行比較, 其餘狀況返回NotImplemmented """
if isinstance(other, A):
print(f'Comparing an A with another A')
return other.value == self.value
if isinstance(other, B):
print(f'Comparing an A with a B')
return other.value == self.value
print('could not compare A with other class')
return NotImplemented
class B:
def __init__(self, value):
self.value = value
def __eq__(self, other):
""" 只有other是B的實例時才調用這個方法進行比較,其餘狀況返回 NotImplemented """
if isinstance(other, B):
print(f'Comparing an B with another B')
return other.value == self.value
print('could not compare B with other class')
return NotImplemented
複製代碼
在代碼中實例化兩個類,分別進行比較,查看調用方法的不一樣:
>>> a1 = A(1)
>>> b1 = B(1)
>>> a1 == b1
Comparing an A with a B
True
>>> a1 == a1
Comparing an A with another A
True
複製代碼
如上例,運行a1 == b1
,首先調用a1
的__eq__()
方法,對先後兩個值a1和b1比較,
一樣a1 == a1
調用A的__eq__()
方法
>>> b1 == b1
Comparing a B with another B
True
複製代碼
調用b1的__eq__()
方法.
>>> b1 == a1
Could not compare B against the other class
Comparing an A with a B
True
複製代碼
如上面代碼所示,b1和a1進行比較時,會首先調用B類中的__eq__()
方法,即b1.__eq__(a1)
,獲得控制檯信息Could not compare B against the other class
,返回NotImpletemented
,這個常量告訴解釋器,對應的方法沒有在該類中實現,讓其嘗試調用a1的__eq__()
方法,獲得控制檯信息Comparing an A with a B
,結果爲Ture
.NorImplemented
自己不會打斷程序的運行.
若是兩個對象的__eq__()
都不能正確得出結果,解釋器會繼續嘗試調用兩個對象的__ne__()
方法,知道獲取正常結果或異常.
對比
NotImplementedError
,NotImplementedError
是一個異常類,表示類的抽象方法或者開發者定義的接口方法,沒有被重寫,而出現的異常,會打斷程序的執行,能夠被捕獲.
class C:
def interface(self):
raise NotImplementedError('you have to overwrite this method')
複製代碼
Ellipsis
該對象的布爾值爲真
>>> bool(Ellipsis)
True
複製代碼
示例:
>>> def func(): Ellipsis
>>> func()
>>> def func(): ...
>>> func()
# 函數的調用結果爲空
複製代碼
對自身的引用:
>>> array = [1, 2, 3]
>>> array.append(array)
>>> print(array)
[1, 2, 3, [...]]
複製代碼
在numpy中被用來模擬數據結果:
>>> import numpy as np
>>> l = np.linspace(1, 100, num=10000)
>>> print(l)
array([ 1. , 1.00990099, 1.01980198, ..., 99.98019802,
99.99009901, 100. ])
複製代碼
__debug__
>>> __debug__ = False
SyntaxError: assignment to keyword
複製代碼
site
模塊添加的常量site
模塊(在啓動期間自動導入,除非給出 -S
命令行選項)將幾個常量添加到內置命名空間。 它們對交互式解釋器 shell 頗有用,而且不該在程序中使用。
quit
(code=None)
exit
(code=None)
quit()
和exit()
表示退出python解釋器
當打印此對象時,會打印出一條消息,例如「Use quit() or Ctrl-D (i.e. EOF) to exit」,當調用此對象時,將使用指定的退出代碼來引起 SystemExit
。
copyright
credits
打印或調用的對象分別打印版權或做者的文本。
license
當打印此對象時,會打印出一條消息「Type license() to see the full license text」,當調用此對象時,將以分頁形式顯示完整的許可證文本(每次顯示一屏)。