當前位置:
首頁 > 知識 > Python對象真值判定邏輯粗解與簡明例子

Python對象真值判定邏輯粗解與簡明例子

Python的真值判定

python3.7

文檔:

https://docs.python.org/3.7/library/stdtypes.html#truth-value-testing

python中有很多時候可以直接將對象用在if和while中.

但是在功能上,這些位置是需要通過Boolean來進行判斷的.

這就牽扯到Python對於True和False的設計概念.

在默認狀態下,python會認定你的對象恆為真直到對象的__bool__()方法返回false或者對象的__len__()返回0.

包括但不限於以下狀況:

None與False

任何數學形式的0:0,0.0,0j,Decimal(0),Fraction(0,1)

空隊列/集合:」,(),[],{},set(),range(0)

以下案例說明了:

1.無__len__()與__bool__()狀態下,python將對象恆判斷為真.

2,3.在有__len__()或__bool__()狀態下,python根據其返回結果來判斷真偽.

4.1,4.2:在__len__()與__bool__()同時存在時,python只會承認__bool__()的結果,無視__len__()的結果.

# 1.在無__len__()與__bool__()狀態下:

class test:

def __init__(self,n):

self.lis = []

for i in range(n):

self.lis.append(i)

def print(self):

try:

print(self.lis.pop())

except:

print("empty list")

=================== RESTART: F:PyWorkspaceooleanTest.py ===================

>>> t = test(9)

>>> while t:

t.print()

8

7

6

5

4

3

2

1

0

empty list

empty list

empty list

empty list

...

# 2.在定義了__len__()的狀態下:

class test:

def __init__(self,n):

self.lis = []

for i in range(n):

self.lis.append(i)

def print(self):

try:

print(self.lis.pop())

except:

print("empty list")

def __len__(self):

return len(self.lis)

=================== RESTART: F:PyWorkspaceooleanTest.py ===================

>>> t = test(10)

>>> while t:

t.print()

9

8

7

6

5

4

3

2

1

0

>>>

# 3.在定義了__bool__()的狀態下:

class test:

def __init__(self,n):

self.flag = False

self.lis = []

for i in range(n):

self.lis.append(i)

self.flag = True

def print(self):

try:

print(self.lis.pop())

except:

print("empty list")

self.flag = False

def __bool__(self):

return self.flag

=================== RESTART: F:PyWorkspaceooleanTest.py ===================

>>> t = test(5)

>>> while t:

t.print()

4

3

2

1

0

empty list

>>>

# 4.1在同時定義了__len__()與__bool__()的狀態下:

class test:

def __init__(self,n):

self.flag = False

self.lis = []

for i in range(n):

self.lis.append(i)

self.flag = True

def print(self):

try:

print(self.lis.pop())

except:

print("empty list")

self.flag = False

def __bool__(self):

return self.flag

def __len__(self):

return len(self.lis)

=================== RESTART: F:PyWorkspaceooleanTest.py ===================

>>> t = test(4)

>>> while t:

t.print()

3

2

1

0

empty list

>>>

# 4.2在同時定義了__len__()與__bool__()的狀態下:

class test:

def __init__(self,n):

self.flag = False

self.lis = []

for i in range(n):

self.lis.append(i)

self.flag = True

def print(self):

try:

print(self.lis.pop())

except:

print("empty list")

self.flag = False

def __bool__(self):

return True

def __len__(self):

return len(self.lis)

=================== RESTART: F:PyWorkspaceooleanTest.py ===================

>>> t = test(4)

>>> while t:

t.print()

3

2

1

0

empty list

empty list

empty list

empty list

empty list

empty list

empty list

...

Python對象真值判定邏輯粗解與簡明例子

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

R 數據處理 list的轉化與轉置
ajax一切正常卻出不來結果怎麼辦?

TAG:程序員小新人學習 |