Should NoneType be iterable?

Cameron Simpson cs at cskk.id.au
Tue Jun 20 19:47:46 EDT 2023


On 21Jun2023 03:01, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>On 20/06/23 7:36 pm, Barry wrote:
>>I have some APIs that do return None or a list.
>>The None says that a list is not available and that the caller is
>>responsible with dealing in a application-domain specific with
>>with that situation.
>
>In that case, the caller should probably be checking for
>None rather than blindly trying to iterate over the result.

I wasted some time the other evening on an API which returned a string 
or None. My own API, and the pain it caused tells me that that API 
design choice isn't good (it's an automatic attribute based on a tag, 
returning None if the tag isn't there). My experience so far is that it 
_looks_ handy so that you can safely say "foo.bar" all the time, but as 
soon a you do something with the value you're in for a world of 
None-checking.

I'm rethinking that choice right now. Just the other day I removed a 
setting in a related class which provided an automatic default value 
because, again, while handy for careless use it caused hard to debug 
problems because the default would flow out the call chain until it was 
unsuitable, making the cause hard to trace.

And of course I'm very -1 on None acquiring iteration or other features.  
Fail early, fail often!

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list