• sugar_in_your_tea@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 days ago

    it’s still implicit

    I don’t see it that way. If you’re doing if len(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 is not None - empty values are explicitly acceptable
    • if not foo - the difference between an empty and None value isn’t important
    • if len(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 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.