Question presented to candidate:
"When you write if something: in Python, which values count as false? And how does Python decide that for an object of your own class?"
What a strong answer should cover:
- The built-in falsy set:
None,False, numeric zero of any type (0,0.0,-0.0,0j,Decimal(0),Fraction(0)), and empty containers ("",b"",(),[],{},set(),frozenset(),range(0),bytearray()); everything else is truthy. - The protocol:
bool(x)calls__bool__if defined (it must return a realbool), otherwise__len__(falsy when it is 0), otherwise the object is truthy. - Truthy surprises:
"0","False"," ",[0],[[]]andfloat("nan")are all truthy. and/orshort-circuit and return one of their operands, notTrue/False, which powersx or default.- The classic bug:
if not x:treats a legitimate0or""as missing; useif x is None:when absence and emptiness differ.
Code / implementation expected: Optional β a quick bool() table or a small class with __bool__ / __len__.