This is more of a philosophical question.
In python, bool([]) evaluates to False.
On the other hand, consider the following:
from dataclasses import dataclass
@dataclass
class Lists:
items: list[str]
other_items: list[str]
assert bool(Lists(items=[], other_items=[]))
The above snippet doesn't raise AssertionError.
Why is that?
I encountered a use-case where it seems to make sense to deduce an object's truth value by aggregating the truth values of its attributes.
Are there any caveats to keep in mind before doing so?
(I'm talking about data classes, like dataclass or pydantic.BaseModel)
__bool__method.None,[], and0has the truth value ofFalse. I know the technicalities of__bool__- my question is more about why libraries likepydanticanddataclasseschose to implement it the way they did.__bool__()method that returnsFalseor a__len__()method that returns zero, when called with the object. docs.python.org/3/library/stdtypes.html#truth-value-testing__bool__option.