I don’t see it that way. If you’re doing iflen(foo)== 0, you’re implying that foo is expected to not be None, and expecting an exception should not be the default assumption, because exceptions should be… exceptional.
Here’s what I assume:
if foo isnotNone - empty values are explicitly acceptable
if not foo - the difference between an empty and None value isn’t important
iflen(foo)== 0 - implicit assumption that foo is not None (I frequently forget that len(...) raises on None)
If an exception was intended by the last bullet point, I prefer an explicit raise:
if foo isNone:
raise ValueError("foo may not be None")
I actually use schema validation to enforce this at the edge so the rest of my code can make reasonable assumptions, and I’m explicit about whether each field may or may not be None.
I don’t see it that way. If you’re doing
if len(foo) == 0
, you’re implying thatfoo
is expected to not beNone
, and expecting an exception should not be the default assumption, because exceptions should be… exceptional.Here’s what I assume:
if foo is not None
- empty values are explicitly acceptableif not foo
- the difference between an empty andNone
value isn’t importantif len(foo) == 0
- implicit assumption thatfoo
is notNone
(I frequently forget thatlen(...)
raises onNone
)If an exception was intended by the last bullet point, I prefer an explicit raise:
if foo is None: raise ValueError("foo may not be None")
I actually use schema validation to enforce this at the edge so the rest of my code can make reasonable assumptions, and I’m explicit about whether each field may or may not be
None
.