python-內建常量,python-常量

來源:互聯網
上載者:User

python-內建常量,python-常量
引言

Python的內建常量不多,只有6個,分別是True、False、None、NotImplemented、Ellipsis、__debug__

一.True

  1.True是bool類型用來表示的真值常量

>>> TrueTrue>>> type(True)<class 'bool'>

  2.對常量True進行任何賦值操作都會拋出語法錯誤

>>> True = 1  File "<stdin>", line 1SyntaxError: can't assign to keyword
二.False

  1.False是bool類型表示假值的常量

>>> FalseFalse>>> type(False)<class 'bool'>

  2.對常量False進行任何賦值操作都會拋出語法錯誤

>>> False = 0  File "<stdin>", line 1SyntaxError: can't assign to keyword
三.None

  1.None表示無,他是NoneType的唯一值

>>> None>>> type(None)<class 'NoneType'>

  2.對常量None進行任何賦值操作都會拋出語法錯誤

>>> None = 1  File "<stdin>", line 1SyntaxError: can't assign to keyword

  3.對於函數,如果沒有return語句,即相當於返回None

>>> def sayhello():  #定義函數...     print('hello world')... >>> >>> sayhello()hello world>>> ret = sayhello()hello world>>> print(ret)None>>> 
四.NotImplemented

  1.NotImplemented是NotImplementedType類型的常量

>>> NotImplementedNotImplemented>>> type(NotImplemented)<class 'NotImplementedType'>

  2.使用bool()函數進行測試可以發現,NotImplemented是一個真值

>>> bool(NotImplemented)True

  3.NotImplemented並不是一個絕對意義上的常量,因為他可以被賦值而不拋出語法錯誤,我們也不應該對其進行賦值,否則會影響程式的執行效果

>>> bool(NotImplemented)True>>> NotImplemented = False

  4.NotImplemented多用於對一些二元特殊方法(比如:__eq__,__It__等)中作為傳回值,表明沒有實現方法,而Python在結果返回NotImplemented時會聰明的互動兩個參數進行另外的嘗試

class A(object):    def __init__(self,name,value):        self.name  = name        self.value = value    def __eq__(self, other):        print('self:',self.name,self.value)        print('other:',other.name,other.value)        return self.value == other.value  #判斷兩個對象的value值是否相等a1 = A('Tome',1)a2 = A('Jim',1)print(a1 == a2)#輸出#self: Tome 1#other: Jim 1#True
class A(object):    def __init__(self,name,value):        self.name  = name        self.value = value    def __eq__(self, other):        print('self:',self.name,self.value)        print('other:',other.name,other.value)        return NotImplementeda1 = A('Tome',1)a2 = A('Jim',1)print(a1 == a2)#輸出# self: Tome 1# other: Jim 1# self: Jim 1# other: Tome 1# False

  當執行a1 == a2時(即調用__eq__(a1,a2)),返回NotImplemented時,Python會自動交換參數再次調用__eq__(a2,a1)

五.Ellipsis

  1.Ellipsis是EllipsisType類型的常量,它和...是等價的

>>> EllipsisEllipsis>>> type(Ellipsis)<class 'ellipsis'>>>> ...Ellipsis>>> ... == EllipsisTrue

  2.使用bool()函數進行測試可以發現,Ellipsis是一個真值

>>> bool(Ellipsis)True

  3. Ellipsis不是一個絕對意義上的常量,因為他可以被賦值卻不會拋出語法錯誤,我們也不應該去對其賦值,否則會影響程式的執行結果。

>>> bool(Ellipsis)True>>> Ellipsis = False>>> bool(Ellipsis)False

  4.Ellipsis多用於表示迴圈的資料結構

>>> a = [1,2,3,4]>>> a.append(a)>>> a[1, 2, 3, 4, [...]]>>> len(a)5>>> a[4][1, 2, 3, 4, [...]]>>> a[4][4][1, 2, 3, 4, [...]]
六.__debug__

  1.__debug__是一個bool類型的常量

>>> __debug__True>>> type(__debug__)<class 'bool'>

  2.對常量__debug__進行任何賦值操作,都會拋出語法錯誤

>>> __debug__ = False  File "<stdin>", line 1SyntaxError: assignment to keyword

  3.如果Python沒有使用-O選項啟動,此常量是真值,否則是假值。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.