3 回答

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
if len(my_list) != 0: print "Not empty!"
if my_list: print "Not empty!"
[]
[1]

TA貢獻(xiàn)1841條經(jīng)驗(yàn) 獲得超3個(gè)贊
None
False
0
0.0
0j
Decimal(0)
Fraction(0, 1)
[]
-空蕩蕩的 list
{}
-空蕩蕩的 dict
()
-空蕩蕩的 tuple
''
-空蕩蕩的 str
b''
-空蕩蕩的 bytes
set()
-空蕩蕩的 set
空蕩蕩的 range
,就像 range(0)
對(duì)象 obj.__bool__()
回報(bào) False
obj.__len__()
回報(bào) 0
if
while
bool
True
False
.

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
bool()
True
False
if
while
.
Vector2d
False
True
.
import mathclass Vector2d(object): def __init__(self, x, y): self.x = float(x) self.y = float(y) def __abs__(self): return math.hypot(self.x, self.y) def __bool__(self): return bool(abs(self))a = Vector2d(0,0)print(bool(a)) #Falseb = Vector2d(10,0) print(bool(b)) #True
注:__bool__
添加回答
舉報(bào)